ConstantFolding.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. //===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines routines for folding instructions into constants.
  11. //
  12. // Also, to supplement the basic VMCore ConstantExpr simplifications,
  13. // this file defines some additional folding routines that can make use of
  14. // TargetData information. These functions cannot go in VMCore due to library
  15. // dependency issues.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/Analysis/ConstantFolding.h"
  19. #include "llvm/Constants.h"
  20. #include "llvm/DerivedTypes.h"
  21. #include "llvm/Function.h"
  22. #include "llvm/GlobalVariable.h"
  23. #include "llvm/Instructions.h"
  24. #include "llvm/Intrinsics.h"
  25. #include "llvm/Operator.h"
  26. #include "llvm/Analysis/ValueTracking.h"
  27. #include "llvm/Target/TargetData.h"
  28. #include "llvm/ADT/SmallVector.h"
  29. #include "llvm/ADT/StringMap.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/GetElementPtrTypeIterator.h"
  32. #include "llvm/Support/MathExtras.h"
  33. #include "llvm/Support/FEnv.h"
  34. #include <cerrno>
  35. #include <cmath>
  36. using namespace llvm;
  37. //===----------------------------------------------------------------------===//
  38. // Constant Folding internal helper functions
  39. //===----------------------------------------------------------------------===//
  40. /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
  41. /// TargetData. This always returns a non-null constant, but it may be a
  42. /// ConstantExpr if unfoldable.
  43. static Constant *FoldBitCast(Constant *C, const Type *DestTy,
  44. const TargetData &TD) {
  45. // This only handles casts to vectors currently.
  46. const VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
  47. if (DestVTy == 0)
  48. return ConstantExpr::getBitCast(C, DestTy);
  49. // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
  50. // vector so the code below can handle it uniformly.
  51. if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
  52. Constant *Ops = C; // don't take the address of C!
  53. return FoldBitCast(ConstantVector::get(Ops), DestTy, TD);
  54. }
  55. // If this is a bitcast from constant vector -> vector, fold it.
  56. ConstantVector *CV = dyn_cast<ConstantVector>(C);
  57. if (CV == 0)
  58. return ConstantExpr::getBitCast(C, DestTy);
  59. // If the element types match, VMCore can fold it.
  60. unsigned NumDstElt = DestVTy->getNumElements();
  61. unsigned NumSrcElt = CV->getNumOperands();
  62. if (NumDstElt == NumSrcElt)
  63. return ConstantExpr::getBitCast(C, DestTy);
  64. const Type *SrcEltTy = CV->getType()->getElementType();
  65. const Type *DstEltTy = DestVTy->getElementType();
  66. // Otherwise, we're changing the number of elements in a vector, which
  67. // requires endianness information to do the right thing. For example,
  68. // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
  69. // folds to (little endian):
  70. // <4 x i32> <i32 0, i32 0, i32 1, i32 0>
  71. // and to (big endian):
  72. // <4 x i32> <i32 0, i32 0, i32 0, i32 1>
  73. // First thing is first. We only want to think about integer here, so if
  74. // we have something in FP form, recast it as integer.
  75. if (DstEltTy->isFloatingPointTy()) {
  76. // Fold to an vector of integers with same size as our FP type.
  77. unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
  78. const Type *DestIVTy =
  79. VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
  80. // Recursively handle this integer conversion, if possible.
  81. C = FoldBitCast(C, DestIVTy, TD);
  82. if (!C) return ConstantExpr::getBitCast(C, DestTy);
  83. // Finally, VMCore can handle this now that #elts line up.
  84. return ConstantExpr::getBitCast(C, DestTy);
  85. }
  86. // Okay, we know the destination is integer, if the input is FP, convert
  87. // it to integer first.
  88. if (SrcEltTy->isFloatingPointTy()) {
  89. unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
  90. const Type *SrcIVTy =
  91. VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
  92. // Ask VMCore to do the conversion now that #elts line up.
  93. C = ConstantExpr::getBitCast(C, SrcIVTy);
  94. CV = dyn_cast<ConstantVector>(C);
  95. if (!CV) // If VMCore wasn't able to fold it, bail out.
  96. return C;
  97. }
  98. // Now we know that the input and output vectors are both integer vectors
  99. // of the same size, and that their #elements is not the same. Do the
  100. // conversion here, which depends on whether the input or output has
  101. // more elements.
  102. bool isLittleEndian = TD.isLittleEndian();
  103. SmallVector<Constant*, 32> Result;
  104. if (NumDstElt < NumSrcElt) {
  105. // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
  106. Constant *Zero = Constant::getNullValue(DstEltTy);
  107. unsigned Ratio = NumSrcElt/NumDstElt;
  108. unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
  109. unsigned SrcElt = 0;
  110. for (unsigned i = 0; i != NumDstElt; ++i) {
  111. // Build each element of the result.
  112. Constant *Elt = Zero;
  113. unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
  114. for (unsigned j = 0; j != Ratio; ++j) {
  115. Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(SrcElt++));
  116. if (!Src) // Reject constantexpr elements.
  117. return ConstantExpr::getBitCast(C, DestTy);
  118. // Zero extend the element to the right size.
  119. Src = ConstantExpr::getZExt(Src, Elt->getType());
  120. // Shift it to the right place, depending on endianness.
  121. Src = ConstantExpr::getShl(Src,
  122. ConstantInt::get(Src->getType(), ShiftAmt));
  123. ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
  124. // Mix it in.
  125. Elt = ConstantExpr::getOr(Elt, Src);
  126. }
  127. Result.push_back(Elt);
  128. }
  129. } else {
  130. // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
  131. unsigned Ratio = NumDstElt/NumSrcElt;
  132. unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits();
  133. // Loop over each source value, expanding into multiple results.
  134. for (unsigned i = 0; i != NumSrcElt; ++i) {
  135. Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(i));
  136. if (!Src) // Reject constantexpr elements.
  137. return ConstantExpr::getBitCast(C, DestTy);
  138. unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
  139. for (unsigned j = 0; j != Ratio; ++j) {
  140. // Shift the piece of the value into the right place, depending on
  141. // endianness.
  142. Constant *Elt = ConstantExpr::getLShr(Src,
  143. ConstantInt::get(Src->getType(), ShiftAmt));
  144. ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
  145. // Truncate and remember this piece.
  146. Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
  147. }
  148. }
  149. }
  150. return ConstantVector::get(Result);
  151. }
  152. /// IsConstantOffsetFromGlobal - If this constant is actually a constant offset
  153. /// from a global, return the global and the constant. Because of
  154. /// constantexprs, this function is recursive.
  155. static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
  156. int64_t &Offset, const TargetData &TD) {
  157. // Trivial case, constant is the global.
  158. if ((GV = dyn_cast<GlobalValue>(C))) {
  159. Offset = 0;
  160. return true;
  161. }
  162. // Otherwise, if this isn't a constant expr, bail out.
  163. ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
  164. if (!CE) return false;
  165. // Look through ptr->int and ptr->ptr casts.
  166. if (CE->getOpcode() == Instruction::PtrToInt ||
  167. CE->getOpcode() == Instruction::BitCast)
  168. return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD);
  169. // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
  170. if (CE->getOpcode() == Instruction::GetElementPtr) {
  171. // Cannot compute this if the element type of the pointer is missing size
  172. // info.
  173. if (!cast<PointerType>(CE->getOperand(0)->getType())
  174. ->getElementType()->isSized())
  175. return false;
  176. // If the base isn't a global+constant, we aren't either.
  177. if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD))
  178. return false;
  179. // Otherwise, add any offset that our operands provide.
  180. gep_type_iterator GTI = gep_type_begin(CE);
  181. for (User::const_op_iterator i = CE->op_begin() + 1, e = CE->op_end();
  182. i != e; ++i, ++GTI) {
  183. ConstantInt *CI = dyn_cast<ConstantInt>(*i);
  184. if (!CI) return false; // Index isn't a simple constant?
  185. if (CI->isZero()) continue; // Not adding anything.
  186. if (const StructType *ST = dyn_cast<StructType>(*GTI)) {
  187. // N = N + Offset
  188. Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue());
  189. } else {
  190. const SequentialType *SQT = cast<SequentialType>(*GTI);
  191. Offset += TD.getTypeAllocSize(SQT->getElementType())*CI->getSExtValue();
  192. }
  193. }
  194. return true;
  195. }
  196. return false;
  197. }
  198. /// ReadDataFromGlobal - Recursive helper to read bits out of global. C is the
  199. /// constant being copied out of. ByteOffset is an offset into C. CurPtr is the
  200. /// pointer to copy results into and BytesLeft is the number of bytes left in
  201. /// the CurPtr buffer. TD is the target data.
  202. static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset,
  203. unsigned char *CurPtr, unsigned BytesLeft,
  204. const TargetData &TD) {
  205. assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) &&
  206. "Out of range access");
  207. // If this element is zero or undefined, we can just return since *CurPtr is
  208. // zero initialized.
  209. if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
  210. return true;
  211. if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
  212. if (CI->getBitWidth() > 64 ||
  213. (CI->getBitWidth() & 7) != 0)
  214. return false;
  215. uint64_t Val = CI->getZExtValue();
  216. unsigned IntBytes = unsigned(CI->getBitWidth()/8);
  217. for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
  218. CurPtr[i] = (unsigned char)(Val >> (ByteOffset * 8));
  219. ++ByteOffset;
  220. }
  221. return true;
  222. }
  223. if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
  224. if (CFP->getType()->isDoubleTy()) {
  225. C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), TD);
  226. return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
  227. }
  228. if (CFP->getType()->isFloatTy()){
  229. C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), TD);
  230. return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
  231. }
  232. return false;
  233. }
  234. if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
  235. const StructLayout *SL = TD.getStructLayout(CS->getType());
  236. unsigned Index = SL->getElementContainingOffset(ByteOffset);
  237. uint64_t CurEltOffset = SL->getElementOffset(Index);
  238. ByteOffset -= CurEltOffset;
  239. while (1) {
  240. // If the element access is to the element itself and not to tail padding,
  241. // read the bytes from the element.
  242. uint64_t EltSize = TD.getTypeAllocSize(CS->getOperand(Index)->getType());
  243. if (ByteOffset < EltSize &&
  244. !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
  245. BytesLeft, TD))
  246. return false;
  247. ++Index;
  248. // Check to see if we read from the last struct element, if so we're done.
  249. if (Index == CS->getType()->getNumElements())
  250. return true;
  251. // If we read all of the bytes we needed from this element we're done.
  252. uint64_t NextEltOffset = SL->getElementOffset(Index);
  253. if (BytesLeft <= NextEltOffset-CurEltOffset-ByteOffset)
  254. return true;
  255. // Move to the next element of the struct.
  256. CurPtr += NextEltOffset-CurEltOffset-ByteOffset;
  257. BytesLeft -= NextEltOffset-CurEltOffset-ByteOffset;
  258. ByteOffset = 0;
  259. CurEltOffset = NextEltOffset;
  260. }
  261. // not reached.
  262. }
  263. if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
  264. uint64_t EltSize = TD.getTypeAllocSize(CA->getType()->getElementType());
  265. uint64_t Index = ByteOffset / EltSize;
  266. uint64_t Offset = ByteOffset - Index * EltSize;
  267. for (; Index != CA->getType()->getNumElements(); ++Index) {
  268. if (!ReadDataFromGlobal(CA->getOperand(Index), Offset, CurPtr,
  269. BytesLeft, TD))
  270. return false;
  271. if (EltSize >= BytesLeft)
  272. return true;
  273. Offset = 0;
  274. BytesLeft -= EltSize;
  275. CurPtr += EltSize;
  276. }
  277. return true;
  278. }
  279. if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
  280. uint64_t EltSize = TD.getTypeAllocSize(CV->getType()->getElementType());
  281. uint64_t Index = ByteOffset / EltSize;
  282. uint64_t Offset = ByteOffset - Index * EltSize;
  283. for (; Index != CV->getType()->getNumElements(); ++Index) {
  284. if (!ReadDataFromGlobal(CV->getOperand(Index), Offset, CurPtr,
  285. BytesLeft, TD))
  286. return false;
  287. if (EltSize >= BytesLeft)
  288. return true;
  289. Offset = 0;
  290. BytesLeft -= EltSize;
  291. CurPtr += EltSize;
  292. }
  293. return true;
  294. }
  295. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
  296. if (CE->getOpcode() == Instruction::IntToPtr &&
  297. CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getContext()))
  298. return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
  299. BytesLeft, TD);
  300. }
  301. // Otherwise, unknown initializer type.
  302. return false;
  303. }
  304. static Constant *FoldReinterpretLoadFromConstPtr(Constant *C,
  305. const TargetData &TD) {
  306. const Type *LoadTy = cast<PointerType>(C->getType())->getElementType();
  307. const IntegerType *IntType = dyn_cast<IntegerType>(LoadTy);
  308. // If this isn't an integer load we can't fold it directly.
  309. if (!IntType) {
  310. // If this is a float/double load, we can try folding it as an int32/64 load
  311. // and then bitcast the result. This can be useful for union cases. Note
  312. // that address spaces don't matter here since we're not going to result in
  313. // an actual new load.
  314. const Type *MapTy;
  315. if (LoadTy->isFloatTy())
  316. MapTy = Type::getInt32PtrTy(C->getContext());
  317. else if (LoadTy->isDoubleTy())
  318. MapTy = Type::getInt64PtrTy(C->getContext());
  319. else if (LoadTy->isVectorTy()) {
  320. MapTy = IntegerType::get(C->getContext(),
  321. TD.getTypeAllocSizeInBits(LoadTy));
  322. MapTy = PointerType::getUnqual(MapTy);
  323. } else
  324. return 0;
  325. C = FoldBitCast(C, MapTy, TD);
  326. if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, TD))
  327. return FoldBitCast(Res, LoadTy, TD);
  328. return 0;
  329. }
  330. unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
  331. if (BytesLoaded > 32 || BytesLoaded == 0) return 0;
  332. GlobalValue *GVal;
  333. int64_t Offset;
  334. if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD))
  335. return 0;
  336. GlobalVariable *GV = dyn_cast<GlobalVariable>(GVal);
  337. if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
  338. !GV->getInitializer()->getType()->isSized())
  339. return 0;
  340. // If we're loading off the beginning of the global, some bytes may be valid,
  341. // but we don't try to handle this.
  342. if (Offset < 0) return 0;
  343. // If we're not accessing anything in this constant, the result is undefined.
  344. if (uint64_t(Offset) >= TD.getTypeAllocSize(GV->getInitializer()->getType()))
  345. return UndefValue::get(IntType);
  346. unsigned char RawBytes[32] = {0};
  347. if (!ReadDataFromGlobal(GV->getInitializer(), Offset, RawBytes,
  348. BytesLoaded, TD))
  349. return 0;
  350. APInt ResultVal = APInt(IntType->getBitWidth(), RawBytes[BytesLoaded-1]);
  351. for (unsigned i = 1; i != BytesLoaded; ++i) {
  352. ResultVal <<= 8;
  353. ResultVal |= RawBytes[BytesLoaded-1-i];
  354. }
  355. return ConstantInt::get(IntType->getContext(), ResultVal);
  356. }
  357. /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
  358. /// produce if it is constant and determinable. If this is not determinable,
  359. /// return null.
  360. Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C,
  361. const TargetData *TD) {
  362. // First, try the easy cases:
  363. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
  364. if (GV->isConstant() && GV->hasDefinitiveInitializer())
  365. return GV->getInitializer();
  366. // If the loaded value isn't a constant expr, we can't handle it.
  367. ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
  368. if (!CE) return 0;
  369. if (CE->getOpcode() == Instruction::GetElementPtr) {
  370. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
  371. if (GV->isConstant() && GV->hasDefinitiveInitializer())
  372. if (Constant *V =
  373. ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
  374. return V;
  375. }
  376. // Instead of loading constant c string, use corresponding integer value
  377. // directly if string length is small enough.
  378. std::string Str;
  379. if (TD && GetConstantStringInfo(CE, Str) && !Str.empty()) {
  380. unsigned StrLen = Str.length();
  381. const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
  382. unsigned NumBits = Ty->getPrimitiveSizeInBits();
  383. // Replace load with immediate integer if the result is an integer or fp
  384. // value.
  385. if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
  386. (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
  387. APInt StrVal(NumBits, 0);
  388. APInt SingleChar(NumBits, 0);
  389. if (TD->isLittleEndian()) {
  390. for (signed i = StrLen-1; i >= 0; i--) {
  391. SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
  392. StrVal = (StrVal << 8) | SingleChar;
  393. }
  394. } else {
  395. for (unsigned i = 0; i < StrLen; i++) {
  396. SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
  397. StrVal = (StrVal << 8) | SingleChar;
  398. }
  399. // Append NULL at the end.
  400. SingleChar = 0;
  401. StrVal = (StrVal << 8) | SingleChar;
  402. }
  403. Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
  404. if (Ty->isFloatingPointTy())
  405. Res = ConstantExpr::getBitCast(Res, Ty);
  406. return Res;
  407. }
  408. }
  409. // If this load comes from anywhere in a constant global, and if the global
  410. // is all undef or zero, we know what it loads.
  411. if (GlobalVariable *GV =
  412. dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, TD))) {
  413. if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
  414. const Type *ResTy = cast<PointerType>(C->getType())->getElementType();
  415. if (GV->getInitializer()->isNullValue())
  416. return Constant::getNullValue(ResTy);
  417. if (isa<UndefValue>(GV->getInitializer()))
  418. return UndefValue::get(ResTy);
  419. }
  420. }
  421. // Try hard to fold loads from bitcasted strange and non-type-safe things. We
  422. // currently don't do any of this for big endian systems. It can be
  423. // generalized in the future if someone is interested.
  424. if (TD && TD->isLittleEndian())
  425. return FoldReinterpretLoadFromConstPtr(CE, *TD);
  426. return 0;
  427. }
  428. static Constant *ConstantFoldLoadInst(const LoadInst *LI, const TargetData *TD){
  429. if (LI->isVolatile()) return 0;
  430. if (Constant *C = dyn_cast<Constant>(LI->getOperand(0)))
  431. return ConstantFoldLoadFromConstPtr(C, TD);
  432. return 0;
  433. }
  434. /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression.
  435. /// Attempt to symbolically evaluate the result of a binary operator merging
  436. /// these together. If target data info is available, it is provided as TD,
  437. /// otherwise TD is null.
  438. static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
  439. Constant *Op1, const TargetData *TD){
  440. // SROA
  441. // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
  442. // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
  443. // bits.
  444. // If the constant expr is something like &A[123] - &A[4].f, fold this into a
  445. // constant. This happens frequently when iterating over a global array.
  446. if (Opc == Instruction::Sub && TD) {
  447. GlobalValue *GV1, *GV2;
  448. int64_t Offs1, Offs2;
  449. if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD))
  450. if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) &&
  451. GV1 == GV2) {
  452. // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
  453. return ConstantInt::get(Op0->getType(), Offs1-Offs2);
  454. }
  455. }
  456. return 0;
  457. }
  458. /// CastGEPIndices - If array indices are not pointer-sized integers,
  459. /// explicitly cast them so that they aren't implicitly casted by the
  460. /// getelementptr.
  461. static Constant *CastGEPIndices(Constant *const *Ops, unsigned NumOps,
  462. const Type *ResultTy,
  463. const TargetData *TD) {
  464. if (!TD) return 0;
  465. const Type *IntPtrTy = TD->getIntPtrType(ResultTy->getContext());
  466. bool Any = false;
  467. SmallVector<Constant*, 32> NewIdxs;
  468. for (unsigned i = 1; i != NumOps; ++i) {
  469. if ((i == 1 ||
  470. !isa<StructType>(GetElementPtrInst::getIndexedType(Ops[0]->getType(),
  471. reinterpret_cast<Value *const *>(Ops+1),
  472. i-1))) &&
  473. Ops[i]->getType() != IntPtrTy) {
  474. Any = true;
  475. NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],
  476. true,
  477. IntPtrTy,
  478. true),
  479. Ops[i], IntPtrTy));
  480. } else
  481. NewIdxs.push_back(Ops[i]);
  482. }
  483. if (!Any) return 0;
  484. Constant *C =
  485. ConstantExpr::getGetElementPtr(Ops[0], &NewIdxs[0], NewIdxs.size());
  486. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
  487. if (Constant *Folded = ConstantFoldConstantExpression(CE, TD))
  488. C = Folded;
  489. return C;
  490. }
  491. /// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP
  492. /// constant expression, do so.
  493. static Constant *SymbolicallyEvaluateGEP(Constant *const *Ops, unsigned NumOps,
  494. const Type *ResultTy,
  495. const TargetData *TD) {
  496. Constant *Ptr = Ops[0];
  497. if (!TD || !cast<PointerType>(Ptr->getType())->getElementType()->isSized())
  498. return 0;
  499. const Type *IntPtrTy = TD->getIntPtrType(Ptr->getContext());
  500. // If this is a constant expr gep that is effectively computing an
  501. // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
  502. for (unsigned i = 1; i != NumOps; ++i)
  503. if (!isa<ConstantInt>(Ops[i])) {
  504. // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
  505. // "inttoptr (sub (ptrtoint Ptr), V)"
  506. if (NumOps == 2 &&
  507. cast<PointerType>(ResultTy)->getElementType()->isIntegerTy(8)) {
  508. ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[1]);
  509. assert((CE == 0 || CE->getType() == IntPtrTy) &&
  510. "CastGEPIndices didn't canonicalize index types!");
  511. if (CE && CE->getOpcode() == Instruction::Sub &&
  512. CE->getOperand(0)->isNullValue()) {
  513. Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
  514. Res = ConstantExpr::getSub(Res, CE->getOperand(1));
  515. Res = ConstantExpr::getIntToPtr(Res, ResultTy);
  516. if (ConstantExpr *ResCE = dyn_cast<ConstantExpr>(Res))
  517. Res = ConstantFoldConstantExpression(ResCE, TD);
  518. return Res;
  519. }
  520. }
  521. return 0;
  522. }
  523. unsigned BitWidth = TD->getTypeSizeInBits(IntPtrTy);
  524. APInt Offset = APInt(BitWidth,
  525. TD->getIndexedOffset(Ptr->getType(),
  526. (Value**)Ops+1, NumOps-1));
  527. Ptr = cast<Constant>(Ptr->stripPointerCasts());
  528. // If this is a GEP of a GEP, fold it all into a single GEP.
  529. while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
  530. SmallVector<Value *, 4> NestedOps(GEP->op_begin()+1, GEP->op_end());
  531. // Do not try the incorporate the sub-GEP if some index is not a number.
  532. bool AllConstantInt = true;
  533. for (unsigned i = 0, e = NestedOps.size(); i != e; ++i)
  534. if (!isa<ConstantInt>(NestedOps[i])) {
  535. AllConstantInt = false;
  536. break;
  537. }
  538. if (!AllConstantInt)
  539. break;
  540. Ptr = cast<Constant>(GEP->getOperand(0));
  541. Offset += APInt(BitWidth,
  542. TD->getIndexedOffset(Ptr->getType(),
  543. (Value**)NestedOps.data(),
  544. NestedOps.size()));
  545. Ptr = cast<Constant>(Ptr->stripPointerCasts());
  546. }
  547. // If the base value for this address is a literal integer value, fold the
  548. // getelementptr to the resulting integer value casted to the pointer type.
  549. APInt BasePtr(BitWidth, 0);
  550. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
  551. if (CE->getOpcode() == Instruction::IntToPtr)
  552. if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
  553. BasePtr = Base->getValue().zextOrTrunc(BitWidth);
  554. if (Ptr->isNullValue() || BasePtr != 0) {
  555. Constant *C = ConstantInt::get(Ptr->getContext(), Offset+BasePtr);
  556. return ConstantExpr::getIntToPtr(C, ResultTy);
  557. }
  558. // Otherwise form a regular getelementptr. Recompute the indices so that
  559. // we eliminate over-indexing of the notional static type array bounds.
  560. // This makes it easy to determine if the getelementptr is "inbounds".
  561. // Also, this helps GlobalOpt do SROA on GlobalVariables.
  562. const Type *Ty = Ptr->getType();
  563. SmallVector<Constant*, 32> NewIdxs;
  564. do {
  565. if (const SequentialType *ATy = dyn_cast<SequentialType>(Ty)) {
  566. if (ATy->isPointerTy()) {
  567. // The only pointer indexing we'll do is on the first index of the GEP.
  568. if (!NewIdxs.empty())
  569. break;
  570. // Only handle pointers to sized types, not pointers to functions.
  571. if (!ATy->getElementType()->isSized())
  572. return 0;
  573. }
  574. // Determine which element of the array the offset points into.
  575. APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
  576. const IntegerType *IntPtrTy = TD->getIntPtrType(Ty->getContext());
  577. if (ElemSize == 0)
  578. // The element size is 0. This may be [0 x Ty]*, so just use a zero
  579. // index for this level and proceed to the next level to see if it can
  580. // accommodate the offset.
  581. NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
  582. else {
  583. // The element size is non-zero divide the offset by the element
  584. // size (rounding down), to compute the index at this level.
  585. APInt NewIdx = Offset.udiv(ElemSize);
  586. Offset -= NewIdx * ElemSize;
  587. NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
  588. }
  589. Ty = ATy->getElementType();
  590. } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
  591. // Determine which field of the struct the offset points into. The
  592. // getZExtValue is at least as safe as the StructLayout API because we
  593. // know the offset is within the struct at this point.
  594. const StructLayout &SL = *TD->getStructLayout(STy);
  595. unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
  596. NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
  597. ElIdx));
  598. Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
  599. Ty = STy->getTypeAtIndex(ElIdx);
  600. } else {
  601. // We've reached some non-indexable type.
  602. break;
  603. }
  604. } while (Ty != cast<PointerType>(ResultTy)->getElementType());
  605. // If we haven't used up the entire offset by descending the static
  606. // type, then the offset is pointing into the middle of an indivisible
  607. // member, so we can't simplify it.
  608. if (Offset != 0)
  609. return 0;
  610. // Create a GEP.
  611. Constant *C =
  612. ConstantExpr::getGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size());
  613. assert(cast<PointerType>(C->getType())->getElementType() == Ty &&
  614. "Computed GetElementPtr has unexpected type!");
  615. // If we ended up indexing a member with a type that doesn't match
  616. // the type of what the original indices indexed, add a cast.
  617. if (Ty != cast<PointerType>(ResultTy)->getElementType())
  618. C = FoldBitCast(C, ResultTy, *TD);
  619. return C;
  620. }
  621. //===----------------------------------------------------------------------===//
  622. // Constant Folding public APIs
  623. //===----------------------------------------------------------------------===//
  624. /// ConstantFoldInstruction - Try to constant fold the specified instruction.
  625. /// If successful, the constant result is returned, if not, null is returned.
  626. /// Note that this fails if not all of the operands are constant. Otherwise,
  627. /// this function can only fail when attempting to fold instructions like loads
  628. /// and stores, which have no constant expression form.
  629. Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) {
  630. // Handle PHI nodes quickly here...
  631. if (PHINode *PN = dyn_cast<PHINode>(I)) {
  632. Constant *CommonValue = 0;
  633. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  634. Value *Incoming = PN->getIncomingValue(i);
  635. // If the incoming value is undef then skip it. Note that while we could
  636. // skip the value if it is equal to the phi node itself we choose not to
  637. // because that would break the rule that constant folding only applies if
  638. // all operands are constants.
  639. if (isa<UndefValue>(Incoming))
  640. continue;
  641. // If the incoming value is not a constant, or is a different constant to
  642. // the one we saw previously, then give up.
  643. Constant *C = dyn_cast<Constant>(Incoming);
  644. if (!C || (CommonValue && C != CommonValue))
  645. return 0;
  646. CommonValue = C;
  647. }
  648. // If we reach here, all incoming values are the same constant or undef.
  649. return CommonValue ? CommonValue : UndefValue::get(PN->getType());
  650. }
  651. // Scan the operand list, checking to see if they are all constants, if so,
  652. // hand off to ConstantFoldInstOperands.
  653. SmallVector<Constant*, 8> Ops;
  654. for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
  655. if (Constant *Op = dyn_cast<Constant>(*i))
  656. Ops.push_back(Op);
  657. else
  658. return 0; // All operands not constant!
  659. if (const CmpInst *CI = dyn_cast<CmpInst>(I))
  660. return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
  661. TD);
  662. if (const LoadInst *LI = dyn_cast<LoadInst>(I))
  663. return ConstantFoldLoadInst(LI, TD);
  664. if (InsertValueInst *IVI = dyn_cast<InsertValueInst>(I))
  665. return ConstantExpr::getInsertValue(
  666. cast<Constant>(IVI->getAggregateOperand()),
  667. cast<Constant>(IVI->getInsertedValueOperand()),
  668. IVI->idx_begin(), IVI->getNumIndices());
  669. if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I))
  670. return ConstantExpr::getExtractValue(
  671. cast<Constant>(EVI->getAggregateOperand()),
  672. EVI->idx_begin(), EVI->getNumIndices());
  673. return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
  674. Ops.data(), Ops.size(), TD);
  675. }
  676. /// ConstantFoldConstantExpression - Attempt to fold the constant expression
  677. /// using the specified TargetData. If successful, the constant result is
  678. /// result is returned, if not, null is returned.
  679. Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE,
  680. const TargetData *TD) {
  681. SmallVector<Constant*, 8> Ops;
  682. for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end();
  683. i != e; ++i) {
  684. Constant *NewC = cast<Constant>(*i);
  685. // Recursively fold the ConstantExpr's operands.
  686. if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC))
  687. NewC = ConstantFoldConstantExpression(NewCE, TD);
  688. Ops.push_back(NewC);
  689. }
  690. if (CE->isCompare())
  691. return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
  692. TD);
  693. return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(),
  694. Ops.data(), Ops.size(), TD);
  695. }
  696. /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
  697. /// specified opcode and operands. If successful, the constant result is
  698. /// returned, if not, null is returned. Note that this function can fail when
  699. /// attempting to fold instructions like loads and stores, which have no
  700. /// constant expression form.
  701. ///
  702. /// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/etc
  703. /// information, due to only being passed an opcode and operands. Constant
  704. /// folding using this function strips this information.
  705. ///
  706. Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
  707. Constant* const* Ops, unsigned NumOps,
  708. const TargetData *TD) {
  709. // Handle easy binops first.
  710. if (Instruction::isBinaryOp(Opcode)) {
  711. if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1]))
  712. if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD))
  713. return C;
  714. return ConstantExpr::get(Opcode, Ops[0], Ops[1]);
  715. }
  716. switch (Opcode) {
  717. default: return 0;
  718. case Instruction::ICmp:
  719. case Instruction::FCmp: assert(0 && "Invalid for compares");
  720. case Instruction::Call:
  721. if (Function *F = dyn_cast<Function>(Ops[NumOps - 1]))
  722. if (canConstantFoldCallTo(F))
  723. return ConstantFoldCall(F, Ops, NumOps - 1);
  724. return 0;
  725. case Instruction::PtrToInt:
  726. // If the input is a inttoptr, eliminate the pair. This requires knowing
  727. // the width of a pointer, so it can't be done in ConstantExpr::getCast.
  728. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
  729. if (TD && CE->getOpcode() == Instruction::IntToPtr) {
  730. Constant *Input = CE->getOperand(0);
  731. unsigned InWidth = Input->getType()->getScalarSizeInBits();
  732. if (TD->getPointerSizeInBits() < InWidth) {
  733. Constant *Mask =
  734. ConstantInt::get(CE->getContext(), APInt::getLowBitsSet(InWidth,
  735. TD->getPointerSizeInBits()));
  736. Input = ConstantExpr::getAnd(Input, Mask);
  737. }
  738. // Do a zext or trunc to get to the dest size.
  739. return ConstantExpr::getIntegerCast(Input, DestTy, false);
  740. }
  741. }
  742. return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
  743. case Instruction::IntToPtr:
  744. // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
  745. // the int size is >= the ptr size. This requires knowing the width of a
  746. // pointer, so it can't be done in ConstantExpr::getCast.
  747. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0]))
  748. if (TD &&
  749. TD->getPointerSizeInBits() <= CE->getType()->getScalarSizeInBits() &&
  750. CE->getOpcode() == Instruction::PtrToInt)
  751. return FoldBitCast(CE->getOperand(0), DestTy, *TD);
  752. return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
  753. case Instruction::Trunc:
  754. case Instruction::ZExt:
  755. case Instruction::SExt:
  756. case Instruction::FPTrunc:
  757. case Instruction::FPExt:
  758. case Instruction::UIToFP:
  759. case Instruction::SIToFP:
  760. case Instruction::FPToUI:
  761. case Instruction::FPToSI:
  762. return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
  763. case Instruction::BitCast:
  764. if (TD)
  765. return FoldBitCast(Ops[0], DestTy, *TD);
  766. return ConstantExpr::getBitCast(Ops[0], DestTy);
  767. case Instruction::Select:
  768. return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
  769. case Instruction::ExtractElement:
  770. return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
  771. case Instruction::InsertElement:
  772. return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
  773. case Instruction::ShuffleVector:
  774. return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
  775. case Instruction::GetElementPtr:
  776. if (Constant *C = CastGEPIndices(Ops, NumOps, DestTy, TD))
  777. return C;
  778. if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, DestTy, TD))
  779. return C;
  780. return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1);
  781. }
  782. }
  783. /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
  784. /// instruction (icmp/fcmp) with the specified operands. If it fails, it
  785. /// returns a constant expression of the specified operands.
  786. ///
  787. Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
  788. Constant *Ops0, Constant *Ops1,
  789. const TargetData *TD) {
  790. // fold: icmp (inttoptr x), null -> icmp x, 0
  791. // fold: icmp (ptrtoint x), 0 -> icmp x, null
  792. // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
  793. // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
  794. //
  795. // ConstantExpr::getCompare cannot do this, because it doesn't have TD
  796. // around to know if bit truncation is happening.
  797. if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
  798. if (TD && Ops1->isNullValue()) {
  799. const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
  800. if (CE0->getOpcode() == Instruction::IntToPtr) {
  801. // Convert the integer value to the right size to ensure we get the
  802. // proper extension or truncation.
  803. Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
  804. IntPtrTy, false);
  805. Constant *Null = Constant::getNullValue(C->getType());
  806. return ConstantFoldCompareInstOperands(Predicate, C, Null, TD);
  807. }
  808. // Only do this transformation if the int is intptrty in size, otherwise
  809. // there is a truncation or extension that we aren't modeling.
  810. if (CE0->getOpcode() == Instruction::PtrToInt &&
  811. CE0->getType() == IntPtrTy) {
  812. Constant *C = CE0->getOperand(0);
  813. Constant *Null = Constant::getNullValue(C->getType());
  814. return ConstantFoldCompareInstOperands(Predicate, C, Null, TD);
  815. }
  816. }
  817. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
  818. if (TD && CE0->getOpcode() == CE1->getOpcode()) {
  819. const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
  820. if (CE0->getOpcode() == Instruction::IntToPtr) {
  821. // Convert the integer value to the right size to ensure we get the
  822. // proper extension or truncation.
  823. Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
  824. IntPtrTy, false);
  825. Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
  826. IntPtrTy, false);
  827. return ConstantFoldCompareInstOperands(Predicate, C0, C1, TD);
  828. }
  829. // Only do this transformation if the int is intptrty in size, otherwise
  830. // there is a truncation or extension that we aren't modeling.
  831. if ((CE0->getOpcode() == Instruction::PtrToInt &&
  832. CE0->getType() == IntPtrTy &&
  833. CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()))
  834. return ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0),
  835. CE1->getOperand(0), TD);
  836. }
  837. }
  838. // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
  839. // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
  840. if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
  841. CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
  842. Constant *LHS =
  843. ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0), Ops1,TD);
  844. Constant *RHS =
  845. ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(1), Ops1,TD);
  846. unsigned OpC =
  847. Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
  848. Constant *Ops[] = { LHS, RHS };
  849. return ConstantFoldInstOperands(OpC, LHS->getType(), Ops, 2, TD);
  850. }
  851. }
  852. return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
  853. }
  854. /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
  855. /// getelementptr constantexpr, return the constant value being addressed by the
  856. /// constant expression, or null if something is funny and we can't decide.
  857. Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
  858. ConstantExpr *CE) {
  859. if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType()))
  860. return 0; // Do not allow stepping over the value!
  861. // Loop over all of the operands, tracking down which value we are
  862. // addressing...
  863. gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
  864. for (++I; I != E; ++I)
  865. if (const StructType *STy = dyn_cast<StructType>(*I)) {
  866. ConstantInt *CU = cast<ConstantInt>(I.getOperand());
  867. assert(CU->getZExtValue() < STy->getNumElements() &&
  868. "Struct index out of range!");
  869. unsigned El = (unsigned)CU->getZExtValue();
  870. if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
  871. C = CS->getOperand(El);
  872. } else if (isa<ConstantAggregateZero>(C)) {
  873. C = Constant::getNullValue(STy->getElementType(El));
  874. } else if (isa<UndefValue>(C)) {
  875. C = UndefValue::get(STy->getElementType(El));
  876. } else {
  877. return 0;
  878. }
  879. } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) {
  880. if (const ArrayType *ATy = dyn_cast<ArrayType>(*I)) {
  881. if (CI->getZExtValue() >= ATy->getNumElements())
  882. return 0;
  883. if (ConstantArray *CA = dyn_cast<ConstantArray>(C))
  884. C = CA->getOperand(CI->getZExtValue());
  885. else if (isa<ConstantAggregateZero>(C))
  886. C = Constant::getNullValue(ATy->getElementType());
  887. else if (isa<UndefValue>(C))
  888. C = UndefValue::get(ATy->getElementType());
  889. else
  890. return 0;
  891. } else if (const VectorType *VTy = dyn_cast<VectorType>(*I)) {
  892. if (CI->getZExtValue() >= VTy->getNumElements())
  893. return 0;
  894. if (ConstantVector *CP = dyn_cast<ConstantVector>(C))
  895. C = CP->getOperand(CI->getZExtValue());
  896. else if (isa<ConstantAggregateZero>(C))
  897. C = Constant::getNullValue(VTy->getElementType());
  898. else if (isa<UndefValue>(C))
  899. C = UndefValue::get(VTy->getElementType());
  900. else
  901. return 0;
  902. } else {
  903. return 0;
  904. }
  905. } else {
  906. return 0;
  907. }
  908. return C;
  909. }
  910. //===----------------------------------------------------------------------===//
  911. // Constant Folding for Calls
  912. //
  913. /// canConstantFoldCallTo - Return true if its even possible to fold a call to
  914. /// the specified function.
  915. bool
  916. llvm::canConstantFoldCallTo(const Function *F) {
  917. switch (F->getIntrinsicID()) {
  918. case Intrinsic::sqrt:
  919. case Intrinsic::powi:
  920. case Intrinsic::bswap:
  921. case Intrinsic::ctpop:
  922. case Intrinsic::ctlz:
  923. case Intrinsic::cttz:
  924. case Intrinsic::sadd_with_overflow:
  925. case Intrinsic::uadd_with_overflow:
  926. case Intrinsic::ssub_with_overflow:
  927. case Intrinsic::usub_with_overflow:
  928. case Intrinsic::smul_with_overflow:
  929. case Intrinsic::umul_with_overflow:
  930. case Intrinsic::convert_from_fp16:
  931. case Intrinsic::convert_to_fp16:
  932. case Intrinsic::x86_sse_cvtss2si:
  933. case Intrinsic::x86_sse_cvtss2si64:
  934. case Intrinsic::x86_sse_cvttss2si:
  935. case Intrinsic::x86_sse_cvttss2si64:
  936. case Intrinsic::x86_sse2_cvtsd2si:
  937. case Intrinsic::x86_sse2_cvtsd2si64:
  938. case Intrinsic::x86_sse2_cvttsd2si:
  939. case Intrinsic::x86_sse2_cvttsd2si64:
  940. return true;
  941. default:
  942. return false;
  943. case 0: break;
  944. }
  945. if (!F->hasName()) return false;
  946. StringRef Name = F->getName();
  947. // In these cases, the check of the length is required. We don't want to
  948. // return true for a name like "cos\0blah" which strcmp would return equal to
  949. // "cos", but has length 8.
  950. switch (Name[0]) {
  951. default: return false;
  952. case 'a':
  953. return Name == "acos" || Name == "asin" ||
  954. Name == "atan" || Name == "atan2";
  955. case 'c':
  956. return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh";
  957. case 'e':
  958. return Name == "exp" || Name == "exp2";
  959. case 'f':
  960. return Name == "fabs" || Name == "fmod" || Name == "floor";
  961. case 'l':
  962. return Name == "log" || Name == "log10";
  963. case 'p':
  964. return Name == "pow";
  965. case 's':
  966. return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
  967. Name == "sinf" || Name == "sqrtf";
  968. case 't':
  969. return Name == "tan" || Name == "tanh";
  970. }
  971. }
  972. static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
  973. const Type *Ty) {
  974. sys::llvm_fenv_clearexcept();
  975. V = NativeFP(V);
  976. if (sys::llvm_fenv_testexcept()) {
  977. sys::llvm_fenv_clearexcept();
  978. return 0;
  979. }
  980. if (Ty->isFloatTy())
  981. return ConstantFP::get(Ty->getContext(), APFloat((float)V));
  982. if (Ty->isDoubleTy())
  983. return ConstantFP::get(Ty->getContext(), APFloat(V));
  984. llvm_unreachable("Can only constant fold float/double");
  985. return 0; // dummy return to suppress warning
  986. }
  987. static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
  988. double V, double W, const Type *Ty) {
  989. sys::llvm_fenv_clearexcept();
  990. V = NativeFP(V, W);
  991. if (sys::llvm_fenv_testexcept()) {
  992. sys::llvm_fenv_clearexcept();
  993. return 0;
  994. }
  995. if (Ty->isFloatTy())
  996. return ConstantFP::get(Ty->getContext(), APFloat((float)V));
  997. if (Ty->isDoubleTy())
  998. return ConstantFP::get(Ty->getContext(), APFloat(V));
  999. llvm_unreachable("Can only constant fold float/double");
  1000. return 0; // dummy return to suppress warning
  1001. }
  1002. /// ConstantFoldConvertToInt - Attempt to an SSE floating point to integer
  1003. /// conversion of a constant floating point. If roundTowardZero is false, the
  1004. /// default IEEE rounding is used (toward nearest, ties to even). This matches
  1005. /// the behavior of the non-truncating SSE instructions in the default rounding
  1006. /// mode. The desired integer type Ty is used to select how many bits are
  1007. /// available for the result. Returns null if the conversion cannot be
  1008. /// performed, otherwise returns the Constant value resulting from the
  1009. /// conversion.
  1010. static Constant *ConstantFoldConvertToInt(ConstantFP *Op, bool roundTowardZero,
  1011. const Type *Ty) {
  1012. assert(Op && "Called with NULL operand");
  1013. APFloat Val(Op->getValueAPF());
  1014. // All of these conversion intrinsics form an integer of at most 64bits.
  1015. unsigned ResultWidth = cast<IntegerType>(Ty)->getBitWidth();
  1016. assert(ResultWidth <= 64 &&
  1017. "Can only constant fold conversions to 64 and 32 bit ints");
  1018. uint64_t UIntVal;
  1019. bool isExact = false;
  1020. APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
  1021. : APFloat::rmNearestTiesToEven;
  1022. APFloat::opStatus status = Val.convertToInteger(&UIntVal, ResultWidth,
  1023. /*isSigned=*/true, mode,
  1024. &isExact);
  1025. if (status != APFloat::opOK && status != APFloat::opInexact)
  1026. return 0;
  1027. return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true);
  1028. }
  1029. /// ConstantFoldCall - Attempt to constant fold a call to the specified function
  1030. /// with the specified arguments, returning null if unsuccessful.
  1031. Constant *
  1032. llvm::ConstantFoldCall(Function *F,
  1033. Constant *const *Operands, unsigned NumOperands) {
  1034. if (!F->hasName()) return 0;
  1035. StringRef Name = F->getName();
  1036. const Type *Ty = F->getReturnType();
  1037. if (NumOperands == 1) {
  1038. if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
  1039. if (F->getIntrinsicID() == Intrinsic::convert_to_fp16) {
  1040. APFloat Val(Op->getValueAPF());
  1041. bool lost = false;
  1042. Val.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &lost);
  1043. return ConstantInt::get(F->getContext(), Val.bitcastToAPInt());
  1044. }
  1045. if (!Ty->isFloatTy() && !Ty->isDoubleTy())
  1046. return 0;
  1047. /// We only fold functions with finite arguments. Folding NaN and inf is
  1048. /// likely to be aborted with an exception anyway, and some host libms
  1049. /// have known errors raising exceptions.
  1050. if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
  1051. return 0;
  1052. /// Currently APFloat versions of these functions do not exist, so we use
  1053. /// the host native double versions. Float versions are not called
  1054. /// directly but for all these it is true (float)(f((double)arg)) ==
  1055. /// f(arg). Long double not supported yet.
  1056. double V = Ty->isFloatTy() ? (double)Op->getValueAPF().convertToFloat() :
  1057. Op->getValueAPF().convertToDouble();
  1058. switch (Name[0]) {
  1059. case 'a':
  1060. if (Name == "acos")
  1061. return ConstantFoldFP(acos, V, Ty);
  1062. else if (Name == "asin")
  1063. return ConstantFoldFP(asin, V, Ty);
  1064. else if (Name == "atan")
  1065. return ConstantFoldFP(atan, V, Ty);
  1066. break;
  1067. case 'c':
  1068. if (Name == "ceil")
  1069. return ConstantFoldFP(ceil, V, Ty);
  1070. else if (Name == "cos")
  1071. return ConstantFoldFP(cos, V, Ty);
  1072. else if (Name == "cosh")
  1073. return ConstantFoldFP(cosh, V, Ty);
  1074. else if (Name == "cosf")
  1075. return ConstantFoldFP(cos, V, Ty);
  1076. break;
  1077. case 'e':
  1078. if (Name == "exp")
  1079. return ConstantFoldFP(exp, V, Ty);
  1080. if (Name == "exp2") {
  1081. // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
  1082. // C99 library.
  1083. return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
  1084. }
  1085. break;
  1086. case 'f':
  1087. if (Name == "fabs")
  1088. return ConstantFoldFP(fabs, V, Ty);
  1089. else if (Name == "floor")
  1090. return ConstantFoldFP(floor, V, Ty);
  1091. break;
  1092. case 'l':
  1093. if (Name == "log" && V > 0)
  1094. return ConstantFoldFP(log, V, Ty);
  1095. else if (Name == "log10" && V > 0)
  1096. return ConstantFoldFP(log10, V, Ty);
  1097. else if (F->getIntrinsicID() == Intrinsic::sqrt &&
  1098. (Ty->isFloatTy() || Ty->isDoubleTy())) {
  1099. if (V >= -0.0)
  1100. return ConstantFoldFP(sqrt, V, Ty);
  1101. else // Undefined
  1102. return Constant::getNullValue(Ty);
  1103. }
  1104. break;
  1105. case 's':
  1106. if (Name == "sin")
  1107. return ConstantFoldFP(sin, V, Ty);
  1108. else if (Name == "sinh")
  1109. return ConstantFoldFP(sinh, V, Ty);
  1110. else if (Name == "sqrt" && V >= 0)
  1111. return ConstantFoldFP(sqrt, V, Ty);
  1112. else if (Name == "sqrtf" && V >= 0)
  1113. return ConstantFoldFP(sqrt, V, Ty);
  1114. else if (Name == "sinf")
  1115. return ConstantFoldFP(sin, V, Ty);
  1116. break;
  1117. case 't':
  1118. if (Name == "tan")
  1119. return ConstantFoldFP(tan, V, Ty);
  1120. else if (Name == "tanh")
  1121. return ConstantFoldFP(tanh, V, Ty);
  1122. break;
  1123. default:
  1124. break;
  1125. }
  1126. return 0;
  1127. }
  1128. if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
  1129. switch (F->getIntrinsicID()) {
  1130. case Intrinsic::bswap:
  1131. return ConstantInt::get(F->getContext(), Op->getValue().byteSwap());
  1132. case Intrinsic::ctpop:
  1133. return ConstantInt::get(Ty, Op->getValue().countPopulation());
  1134. case Intrinsic::cttz:
  1135. return ConstantInt::get(Ty, Op->getValue().countTrailingZeros());
  1136. case Intrinsic::ctlz:
  1137. return ConstantInt::get(Ty, Op->getValue().countLeadingZeros());
  1138. case Intrinsic::convert_from_fp16: {
  1139. APFloat Val(Op->getValue());
  1140. bool lost = false;
  1141. APFloat::opStatus status =
  1142. Val.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &lost);
  1143. // Conversion is always precise.
  1144. (void)status;
  1145. assert(status == APFloat::opOK && !lost &&
  1146. "Precision lost during fp16 constfolding");
  1147. return ConstantFP::get(F->getContext(), Val);
  1148. }
  1149. default:
  1150. return 0;
  1151. }
  1152. }
  1153. if (ConstantVector *Op = dyn_cast<ConstantVector>(Operands[0])) {
  1154. switch (F->getIntrinsicID()) {
  1155. default: break;
  1156. case Intrinsic::x86_sse_cvtss2si:
  1157. case Intrinsic::x86_sse_cvtss2si64:
  1158. case Intrinsic::x86_sse2_cvtsd2si:
  1159. case Intrinsic::x86_sse2_cvtsd2si64:
  1160. if (ConstantFP *FPOp = dyn_cast<ConstantFP>(Op->getOperand(0)))
  1161. return ConstantFoldConvertToInt(FPOp, /*roundTowardZero=*/false, Ty);
  1162. case Intrinsic::x86_sse_cvttss2si:
  1163. case Intrinsic::x86_sse_cvttss2si64:
  1164. case Intrinsic::x86_sse2_cvttsd2si:
  1165. case Intrinsic::x86_sse2_cvttsd2si64:
  1166. if (ConstantFP *FPOp = dyn_cast<ConstantFP>(Op->getOperand(0)))
  1167. return ConstantFoldConvertToInt(FPOp, /*roundTowardZero=*/true, Ty);
  1168. }
  1169. }
  1170. if (isa<UndefValue>(Operands[0])) {
  1171. if (F->getIntrinsicID() == Intrinsic::bswap)
  1172. return Operands[0];
  1173. return 0;
  1174. }
  1175. return 0;
  1176. }
  1177. if (NumOperands == 2) {
  1178. if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
  1179. if (!Ty->isFloatTy() && !Ty->isDoubleTy())
  1180. return 0;
  1181. double Op1V = Ty->isFloatTy() ?
  1182. (double)Op1->getValueAPF().convertToFloat() :
  1183. Op1->getValueAPF().convertToDouble();
  1184. if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
  1185. if (Op2->getType() != Op1->getType())
  1186. return 0;
  1187. double Op2V = Ty->isFloatTy() ?
  1188. (double)Op2->getValueAPF().convertToFloat():
  1189. Op2->getValueAPF().convertToDouble();
  1190. if (Name == "pow")
  1191. return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
  1192. if (Name == "fmod")
  1193. return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
  1194. if (Name == "atan2")
  1195. return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
  1196. } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
  1197. if (F->getIntrinsicID() == Intrinsic::powi && Ty->isFloatTy())
  1198. return ConstantFP::get(F->getContext(),
  1199. APFloat((float)std::pow((float)Op1V,
  1200. (int)Op2C->getZExtValue())));
  1201. if (F->getIntrinsicID() == Intrinsic::powi && Ty->isDoubleTy())
  1202. return ConstantFP::get(F->getContext(),
  1203. APFloat((double)std::pow((double)Op1V,
  1204. (int)Op2C->getZExtValue())));
  1205. }
  1206. return 0;
  1207. }
  1208. if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
  1209. if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
  1210. switch (F->getIntrinsicID()) {
  1211. default: break;
  1212. case Intrinsic::sadd_with_overflow:
  1213. case Intrinsic::uadd_with_overflow:
  1214. case Intrinsic::ssub_with_overflow:
  1215. case Intrinsic::usub_with_overflow:
  1216. case Intrinsic::smul_with_overflow:
  1217. case Intrinsic::umul_with_overflow: {
  1218. APInt Res;
  1219. bool Overflow;
  1220. switch (F->getIntrinsicID()) {
  1221. default: assert(0 && "Invalid case");
  1222. case Intrinsic::sadd_with_overflow:
  1223. Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow);
  1224. break;
  1225. case Intrinsic::uadd_with_overflow:
  1226. Res = Op1->getValue().uadd_ov(Op2->getValue(), Overflow);
  1227. break;
  1228. case Intrinsic::ssub_with_overflow:
  1229. Res = Op1->getValue().ssub_ov(Op2->getValue(), Overflow);
  1230. break;
  1231. case Intrinsic::usub_with_overflow:
  1232. Res = Op1->getValue().usub_ov(Op2->getValue(), Overflow);
  1233. break;
  1234. case Intrinsic::smul_with_overflow:
  1235. Res = Op1->getValue().smul_ov(Op2->getValue(), Overflow);
  1236. break;
  1237. case Intrinsic::umul_with_overflow:
  1238. Res = Op1->getValue().umul_ov(Op2->getValue(), Overflow);
  1239. break;
  1240. }
  1241. Constant *Ops[] = {
  1242. ConstantInt::get(F->getContext(), Res),
  1243. ConstantInt::get(Type::getInt1Ty(F->getContext()), Overflow)
  1244. };
  1245. return ConstantStruct::get(cast<StructType>(F->getReturnType()), Ops);
  1246. }
  1247. }
  1248. }
  1249. return 0;
  1250. }
  1251. return 0;
  1252. }
  1253. return 0;
  1254. }