diff options
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r-- | llvm/lib/Analysis/ConstantFolding.cpp | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index ace7131e7d5..c778438aec2 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -320,6 +320,41 @@ bool llvm::IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, return true; } +Constant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, + const DataLayout &DL) { + do { + Type *SrcTy = C->getType(); + + // If the type sizes are the same and a cast is legal, just directly + // cast the constant. + if (DL.getTypeSizeInBits(DestTy) == DL.getTypeSizeInBits(SrcTy)) { + Instruction::CastOps Cast = Instruction::BitCast; + // If we are going from a pointer to int or vice versa, we spell the cast + // differently. + if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) + Cast = Instruction::IntToPtr; + else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) + Cast = Instruction::PtrToInt; + + if (CastInst::castIsValid(Cast, C, DestTy)) + return ConstantExpr::getCast(Cast, C, DestTy); + } + + // If this isn't an aggregate type, there is nothing we can do to drill down + // and find a bitcastable constant. + if (!SrcTy->isAggregateType()) + return nullptr; + + // We're simulating a load through a pointer that was bitcast to point to + // a different type, so we can try to walk down through the initial + // elements of an aggregate to see if some part of th e aggregate is + // castable to implement the "load" semantic model. + C = C->getAggregateElement(0u); + } while (C); + + return nullptr; +} + namespace { /// Recursive helper to read bits out of global. C is the constant being copied @@ -537,8 +572,8 @@ Constant *FoldReinterpretLoadFromConstPtr(Constant *C, Type *LoadTy, return ConstantInt::get(IntType->getContext(), ResultVal); } -Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE, Type *DestTy, - const DataLayout &DL) { +Constant *ConstantFoldLoadThroughBitcastExpr(ConstantExpr *CE, Type *DestTy, + const DataLayout &DL) { auto *SrcPtr = CE->getOperand(0); auto *SrcPtrTy = dyn_cast<PointerType>(SrcPtr->getType()); if (!SrcPtrTy) @@ -549,37 +584,7 @@ Constant *ConstantFoldLoadThroughBitcast(ConstantExpr *CE, Type *DestTy, if (!C) return nullptr; - do { - Type *SrcTy = C->getType(); - - // If the type sizes are the same and a cast is legal, just directly - // cast the constant. - if (DL.getTypeSizeInBits(DestTy) == DL.getTypeSizeInBits(SrcTy)) { - Instruction::CastOps Cast = Instruction::BitCast; - // If we are going from a pointer to int or vice versa, we spell the cast - // differently. - if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) - Cast = Instruction::IntToPtr; - else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) - Cast = Instruction::PtrToInt; - - if (CastInst::castIsValid(Cast, C, DestTy)) - return ConstantExpr::getCast(Cast, C, DestTy); - } - - // If this isn't an aggregate type, there is nothing we can do to drill down - // and find a bitcastable constant. - if (!SrcTy->isAggregateType()) - return nullptr; - - // We're simulating a load through a pointer that was bitcast to point to - // a different type, so we can try to walk down through the initial - // elements of an aggregate to see if some part of th e aggregate is - // castable to implement the "load" semantic model. - C = C->getAggregateElement(0u); - } while (C); - - return nullptr; + return llvm::ConstantFoldLoadThroughBitcast(C, DestTy, DL); } } // end anonymous namespace @@ -611,7 +616,7 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, } if (CE->getOpcode() == Instruction::BitCast) - if (Constant *LoadedC = ConstantFoldLoadThroughBitcast(CE, Ty, DL)) + if (Constant *LoadedC = ConstantFoldLoadThroughBitcastExpr(CE, Ty, DL)) return LoadedC; // Instead of loading constant c string, use corresponding integer value |