Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 1 | //===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===// |
| 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 pass statically checks for common and easily-identified constructs |
| 11 | // which produce undefined or likely unintended behavior in LLVM IR. |
| 12 | // |
| 13 | // It is not a guarantee of correctness, in two ways. First, it isn't |
| 14 | // comprehensive. There are checks which could be done statically which are |
| 15 | // not yet implemented. Some of these are indicated by TODO comments, but |
| 16 | // those aren't comprehensive either. Second, many conditions cannot be |
| 17 | // checked statically. This pass does no dynamic instrumentation, so it |
| 18 | // can't check for all possible problems. |
| 19 | // |
| 20 | // Another limitation is that it assumes all code will be executed. A store |
| 21 | // through a null pointer in a basic block which is never reached is harmless, |
Dan Gohman | d3b6e41 | 2010-07-06 15:21:57 +0000 | [diff] [blame] | 22 | // but this pass will warn about it anyway. This is the main reason why most |
| 23 | // of these checks live here instead of in the Verifier pass. |
Dan Gohman | 0883355 | 2010-04-22 01:30:05 +0000 | [diff] [blame] | 24 | // |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 25 | // Optimization passes may make conditions that this pass checks for more or |
| 26 | // less obvious. If an optimization pass appears to be introducing a warning, |
| 27 | // it may be that the optimization pass is merely exposing an existing |
| 28 | // condition in the code. |
| 29 | // |
| 30 | // This code may be run before instcombine. In many cases, instcombine checks |
| 31 | // for the same kinds of things and turns instructions with undefined behavior |
| 32 | // into unreachable (or equivalent). Because of this, this pass makes some |
| 33 | // effort to look through bitcasts and so on. |
| 34 | // |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | #include "llvm/Analysis/Passes.h" |
| 38 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/InstructionSimplify.h" |
| 40 | #include "llvm/Analysis/ConstantFolding.h" |
| 41 | #include "llvm/Analysis/Dominators.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/Lint.h" |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/Loads.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/ValueTracking.h" |
| 45 | #include "llvm/Assembly/Writer.h" |
| 46 | #include "llvm/Target/TargetData.h" |
| 47 | #include "llvm/Pass.h" |
| 48 | #include "llvm/PassManager.h" |
| 49 | #include "llvm/IntrinsicInst.h" |
| 50 | #include "llvm/Function.h" |
| 51 | #include "llvm/Support/CallSite.h" |
| 52 | #include "llvm/Support/Debug.h" |
| 53 | #include "llvm/Support/InstVisitor.h" |
| 54 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 56 | using namespace llvm; |
| 57 | |
| 58 | namespace { |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 59 | namespace MemRef { |
| 60 | static unsigned Read = 1; |
| 61 | static unsigned Write = 2; |
| 62 | static unsigned Callee = 4; |
| 63 | static unsigned Branchee = 8; |
| 64 | } |
| 65 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 66 | class Lint : public FunctionPass, public InstVisitor<Lint> { |
| 67 | friend class InstVisitor<Lint>; |
| 68 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 69 | void visitFunction(Function &F); |
| 70 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 71 | void visitCallSite(CallSite CS); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 72 | void visitMemoryReference(Instruction &I, Value *Ptr, |
| 73 | unsigned Size, unsigned Align, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 74 | const Type *Ty, unsigned Flags); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 75 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 76 | void visitCallInst(CallInst &I); |
| 77 | void visitInvokeInst(InvokeInst &I); |
| 78 | void visitReturnInst(ReturnInst &I); |
| 79 | void visitLoadInst(LoadInst &I); |
| 80 | void visitStoreInst(StoreInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 81 | void visitXor(BinaryOperator &I); |
| 82 | void visitSub(BinaryOperator &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 83 | void visitLShr(BinaryOperator &I); |
| 84 | void visitAShr(BinaryOperator &I); |
| 85 | void visitShl(BinaryOperator &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 86 | void visitSDiv(BinaryOperator &I); |
| 87 | void visitUDiv(BinaryOperator &I); |
| 88 | void visitSRem(BinaryOperator &I); |
| 89 | void visitURem(BinaryOperator &I); |
| 90 | void visitAllocaInst(AllocaInst &I); |
| 91 | void visitVAArgInst(VAArgInst &I); |
| 92 | void visitIndirectBrInst(IndirectBrInst &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 93 | void visitExtractElementInst(ExtractElementInst &I); |
| 94 | void visitInsertElementInst(InsertElementInst &I); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 95 | void visitUnreachableInst(UnreachableInst &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 96 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 97 | Value *findValue(Value *V, bool OffsetOk) const; |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 98 | Value *findValueImpl(Value *V, bool OffsetOk, |
| 99 | SmallPtrSet<Value *, 4> &Visited) const; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 100 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 101 | public: |
| 102 | Module *Mod; |
| 103 | AliasAnalysis *AA; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 104 | DominatorTree *DT; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 105 | TargetData *TD; |
| 106 | |
| 107 | std::string Messages; |
| 108 | raw_string_ostream MessagesStr; |
| 109 | |
| 110 | static char ID; // Pass identification, replacement for typeid |
| 111 | Lint() : FunctionPass(&ID), MessagesStr(Messages) {} |
| 112 | |
| 113 | virtual bool runOnFunction(Function &F); |
| 114 | |
| 115 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 116 | AU.setPreservesAll(); |
| 117 | AU.addRequired<AliasAnalysis>(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 118 | AU.addRequired<DominatorTree>(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 119 | } |
| 120 | virtual void print(raw_ostream &O, const Module *M) const {} |
| 121 | |
| 122 | void WriteValue(const Value *V) { |
| 123 | if (!V) return; |
| 124 | if (isa<Instruction>(V)) { |
| 125 | MessagesStr << *V << '\n'; |
| 126 | } else { |
| 127 | WriteAsOperand(MessagesStr, V, true, Mod); |
| 128 | MessagesStr << '\n'; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void WriteType(const Type *T) { |
| 133 | if (!T) return; |
| 134 | MessagesStr << ' '; |
| 135 | WriteTypeSymbolic(MessagesStr, T, Mod); |
| 136 | } |
| 137 | |
| 138 | // CheckFailed - A check failed, so print out the condition and the message |
| 139 | // that failed. This provides a nice place to put a breakpoint if you want |
| 140 | // to see why something is not correct. |
| 141 | void CheckFailed(const Twine &Message, |
| 142 | const Value *V1 = 0, const Value *V2 = 0, |
| 143 | const Value *V3 = 0, const Value *V4 = 0) { |
| 144 | MessagesStr << Message.str() << "\n"; |
| 145 | WriteValue(V1); |
| 146 | WriteValue(V2); |
| 147 | WriteValue(V3); |
| 148 | WriteValue(V4); |
| 149 | } |
| 150 | |
| 151 | void CheckFailed(const Twine &Message, const Value *V1, |
| 152 | const Type *T2, const Value *V3 = 0) { |
| 153 | MessagesStr << Message.str() << "\n"; |
| 154 | WriteValue(V1); |
| 155 | WriteType(T2); |
| 156 | WriteValue(V3); |
| 157 | } |
| 158 | |
| 159 | void CheckFailed(const Twine &Message, const Type *T1, |
| 160 | const Type *T2 = 0, const Type *T3 = 0) { |
| 161 | MessagesStr << Message.str() << "\n"; |
| 162 | WriteType(T1); |
| 163 | WriteType(T2); |
| 164 | WriteType(T3); |
| 165 | } |
| 166 | }; |
| 167 | } |
| 168 | |
| 169 | char Lint::ID = 0; |
| 170 | static RegisterPass<Lint> |
| 171 | X("lint", "Statically lint-checks LLVM IR", false, true); |
| 172 | |
| 173 | // Assert - We know that cond should be true, if not print an error message. |
| 174 | #define Assert(C, M) \ |
| 175 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
| 176 | #define Assert1(C, M, V1) \ |
| 177 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
| 178 | #define Assert2(C, M, V1, V2) \ |
| 179 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
| 180 | #define Assert3(C, M, V1, V2, V3) \ |
| 181 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 182 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 183 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
| 184 | |
| 185 | // Lint::run - This is the main Analysis entry point for a |
| 186 | // function. |
| 187 | // |
| 188 | bool Lint::runOnFunction(Function &F) { |
| 189 | Mod = F.getParent(); |
| 190 | AA = &getAnalysis<AliasAnalysis>(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 191 | DT = &getAnalysis<DominatorTree>(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 192 | TD = getAnalysisIfAvailable<TargetData>(); |
| 193 | visit(F); |
| 194 | dbgs() << MessagesStr.str(); |
Dan Gohman | a0f7ff3 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 195 | Messages.clear(); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 196 | return false; |
| 197 | } |
| 198 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 199 | void Lint::visitFunction(Function &F) { |
| 200 | // This isn't undefined behavior, it's just a little unusual, and it's a |
| 201 | // fairly common mistake to neglect to name a function. |
| 202 | Assert1(F.hasName() || F.hasLocalLinkage(), |
| 203 | "Unusual: Unnamed function with non-local linkage", &F); |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 204 | |
| 205 | // TODO: Check for irreducible control flow. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void Lint::visitCallSite(CallSite CS) { |
| 209 | Instruction &I = *CS.getInstruction(); |
| 210 | Value *Callee = CS.getCalledValue(); |
| 211 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 212 | visitMemoryReference(I, Callee, ~0u, 0, 0, MemRef::Callee); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 213 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 214 | if (Function *F = dyn_cast<Function>(findValue(Callee, /*OffsetOk=*/false))) { |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 215 | Assert1(CS.getCallingConv() == F->getCallingConv(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 216 | "Undefined behavior: Caller and callee calling convention differ", |
| 217 | &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 218 | |
| 219 | const FunctionType *FT = F->getFunctionType(); |
| 220 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 221 | |
| 222 | Assert1(FT->isVarArg() ? |
| 223 | FT->getNumParams() <= NumActualArgs : |
| 224 | FT->getNumParams() == NumActualArgs, |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 225 | "Undefined behavior: Call argument count mismatches callee " |
| 226 | "argument count", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 227 | |
Dan Gohman | 545d006 | 2010-07-12 18:02:04 +0000 | [diff] [blame^] | 228 | Assert1(FT->getReturnType() == I.getType(), |
| 229 | "Undefined behavior: Call return type mismatches " |
| 230 | "callee return type", &I); |
| 231 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 232 | // Check argument types (in case the callee was casted) and attributes. |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 233 | // TODO: Verify that caller and callee attributes are compatible. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 234 | Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end(); |
| 235 | CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 236 | for (; AI != AE; ++AI) { |
| 237 | Value *Actual = *AI; |
| 238 | if (PI != PE) { |
| 239 | Argument *Formal = PI++; |
| 240 | Assert1(Formal->getType() == Actual->getType(), |
| 241 | "Undefined behavior: Call argument type mismatches " |
| 242 | "callee parameter type", &I); |
Dan Gohman | 10e7726 | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 243 | |
| 244 | // Check that noalias arguments don't alias other arguments. The |
| 245 | // AliasAnalysis API isn't expressive enough for what we really want |
| 246 | // to do. Known partial overlap is not distinguished from the case |
| 247 | // where nothing is known. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 248 | if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy()) |
Dan Gohman | 10e7726 | 2010-06-01 20:51:40 +0000 | [diff] [blame] | 249 | for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI) { |
| 250 | Assert1(AI == BI || |
| 251 | AA->alias(*AI, ~0u, *BI, ~0u) != AliasAnalysis::MustAlias, |
| 252 | "Unusual: noalias argument aliases another argument", &I); |
| 253 | } |
| 254 | |
| 255 | // Check that an sret argument points to valid memory. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 256 | if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) { |
| 257 | const Type *Ty = |
| 258 | cast<PointerType>(Formal->getType())->getElementType(); |
| 259 | visitMemoryReference(I, Actual, AA->getTypeStoreSize(Ty), |
| 260 | TD ? TD->getABITypeAlignment(Ty) : 0, |
| 261 | Ty, MemRef::Read | MemRef::Write); |
| 262 | } |
| 263 | } |
| 264 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 267 | if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall()) |
| 268 | for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); |
| 269 | AI != AE; ++AI) { |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 270 | Value *Obj = findValue(*AI, /*OffsetOk=*/true); |
Dan Gohman | 078f859 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 271 | Assert1(!isa<AllocaInst>(Obj), |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 272 | "Undefined behavior: Call with \"tail\" keyword references " |
Dan Gohman | 078f859 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 273 | "alloca", &I); |
Dan Gohman | 113b3e2 | 2010-05-26 21:46:36 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 276 | |
| 277 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) |
| 278 | switch (II->getIntrinsicID()) { |
| 279 | default: break; |
| 280 | |
| 281 | // TODO: Check more intrinsics |
| 282 | |
| 283 | case Intrinsic::memcpy: { |
| 284 | MemCpyInst *MCI = cast<MemCpyInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 285 | // TODO: If the size is known, use it. |
| 286 | visitMemoryReference(I, MCI->getDest(), ~0u, MCI->getAlignment(), 0, |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 287 | MemRef::Write); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 288 | visitMemoryReference(I, MCI->getSource(), ~0u, MCI->getAlignment(), 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 289 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 290 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 291 | // Check that the memcpy arguments don't overlap. The AliasAnalysis API |
| 292 | // isn't expressive enough for what we really want to do. Known partial |
| 293 | // overlap is not distinguished from the case where nothing is known. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 294 | unsigned Size = 0; |
| 295 | if (const ConstantInt *Len = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 296 | dyn_cast<ConstantInt>(findValue(MCI->getLength(), |
| 297 | /*OffsetOk=*/false))) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 298 | if (Len->getValue().isIntN(32)) |
| 299 | Size = Len->getValue().getZExtValue(); |
| 300 | Assert1(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) != |
| 301 | AliasAnalysis::MustAlias, |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 302 | "Undefined behavior: memcpy source and destination overlap", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | case Intrinsic::memmove: { |
| 306 | MemMoveInst *MMI = cast<MemMoveInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 307 | // TODO: If the size is known, use it. |
| 308 | visitMemoryReference(I, MMI->getDest(), ~0u, MMI->getAlignment(), 0, |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 309 | MemRef::Write); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 310 | visitMemoryReference(I, MMI->getSource(), ~0u, MMI->getAlignment(), 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 311 | MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | case Intrinsic::memset: { |
| 315 | MemSetInst *MSI = cast<MemSetInst>(&I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 316 | // TODO: If the size is known, use it. |
| 317 | visitMemoryReference(I, MSI->getDest(), ~0u, MSI->getAlignment(), 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 318 | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | |
| 322 | case Intrinsic::vastart: |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 323 | Assert1(I.getParent()->getParent()->isVarArg(), |
| 324 | "Undefined behavior: va_start called in a non-varargs function", |
| 325 | &I); |
| 326 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 327 | visitMemoryReference(I, CS.getArgument(0), ~0u, 0, 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 328 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 329 | break; |
| 330 | case Intrinsic::vacopy: |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 331 | visitMemoryReference(I, CS.getArgument(0), ~0u, 0, 0, MemRef::Write); |
| 332 | visitMemoryReference(I, CS.getArgument(1), ~0u, 0, 0, MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 333 | break; |
| 334 | case Intrinsic::vaend: |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 335 | visitMemoryReference(I, CS.getArgument(0), ~0u, 0, 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 336 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 337 | break; |
Dan Gohman | 882ddb4 | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 338 | |
| 339 | case Intrinsic::stackrestore: |
| 340 | // Stackrestore doesn't read or write memory, but it sets the |
| 341 | // stack pointer, which the compiler may read from or write to |
| 342 | // at any time, so check it for both readability and writeability. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 343 | visitMemoryReference(I, CS.getArgument(0), ~0u, 0, 0, |
Dan Gohman | 882ddb4 | 2010-05-26 22:21:25 +0000 | [diff] [blame] | 344 | MemRef::Read | MemRef::Write); |
| 345 | break; |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | void Lint::visitCallInst(CallInst &I) { |
| 350 | return visitCallSite(&I); |
| 351 | } |
| 352 | |
| 353 | void Lint::visitInvokeInst(InvokeInst &I) { |
| 354 | return visitCallSite(&I); |
| 355 | } |
| 356 | |
| 357 | void Lint::visitReturnInst(ReturnInst &I) { |
| 358 | Function *F = I.getParent()->getParent(); |
| 359 | Assert1(!F->doesNotReturn(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 360 | "Unusual: Return statement in function with noreturn attribute", |
| 361 | &I); |
Dan Gohman | 292fc87 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 362 | |
| 363 | if (Value *V = I.getReturnValue()) { |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 364 | Value *Obj = findValue(V, /*OffsetOk=*/true); |
Dan Gohman | 078f859 | 2010-05-28 16:34:49 +0000 | [diff] [blame] | 365 | Assert1(!isa<AllocaInst>(Obj), |
| 366 | "Unusual: Returning alloca value", &I); |
Dan Gohman | 292fc87 | 2010-05-28 04:33:42 +0000 | [diff] [blame] | 367 | } |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 370 | // TODO: Check that the reference is in bounds. |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 371 | // TODO: Check readnone/readonly function attributes. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 372 | void Lint::visitMemoryReference(Instruction &I, |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 373 | Value *Ptr, unsigned Size, unsigned Align, |
| 374 | const Type *Ty, unsigned Flags) { |
| 375 | // If no memory is being referenced, it doesn't matter if the pointer |
| 376 | // is valid. |
| 377 | if (Size == 0) |
| 378 | return; |
| 379 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 380 | Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true); |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 381 | Assert1(!isa<ConstantPointerNull>(UnderlyingObject), |
| 382 | "Undefined behavior: Null pointer dereference", &I); |
| 383 | Assert1(!isa<UndefValue>(UnderlyingObject), |
| 384 | "Undefined behavior: Undef pointer dereference", &I); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 385 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 386 | !cast<ConstantInt>(UnderlyingObject)->isAllOnesValue(), |
| 387 | "Unusual: All-ones pointer dereference", &I); |
| 388 | Assert1(!isa<ConstantInt>(UnderlyingObject) || |
| 389 | !cast<ConstantInt>(UnderlyingObject)->isOne(), |
| 390 | "Unusual: Address one pointer dereference", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 391 | |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 392 | if (Flags & MemRef::Write) { |
| 393 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject)) |
| 394 | Assert1(!GV->isConstant(), |
| 395 | "Undefined behavior: Write to read-only memory", &I); |
| 396 | Assert1(!isa<Function>(UnderlyingObject) && |
| 397 | !isa<BlockAddress>(UnderlyingObject), |
| 398 | "Undefined behavior: Write to text section", &I); |
| 399 | } |
| 400 | if (Flags & MemRef::Read) { |
| 401 | Assert1(!isa<Function>(UnderlyingObject), |
| 402 | "Unusual: Load from function body", &I); |
| 403 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 404 | "Undefined behavior: Load from block address", &I); |
| 405 | } |
| 406 | if (Flags & MemRef::Callee) { |
| 407 | Assert1(!isa<BlockAddress>(UnderlyingObject), |
| 408 | "Undefined behavior: Call to block address", &I); |
| 409 | } |
| 410 | if (Flags & MemRef::Branchee) { |
| 411 | Assert1(!isa<Constant>(UnderlyingObject) || |
| 412 | isa<BlockAddress>(UnderlyingObject), |
| 413 | "Undefined behavior: Branch to non-blockaddress", &I); |
| 414 | } |
| 415 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 416 | if (TD) { |
| 417 | if (Align == 0 && Ty) Align = TD->getABITypeAlignment(Ty); |
| 418 | |
| 419 | if (Align != 0) { |
| 420 | unsigned BitWidth = TD->getTypeSizeInBits(Ptr->getType()); |
| 421 | APInt Mask = APInt::getAllOnesValue(BitWidth), |
| 422 | KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 423 | ComputeMaskedBits(Ptr, Mask, KnownZero, KnownOne, TD); |
| 424 | Assert1(!(KnownOne & APInt::getLowBitsSet(BitWidth, Log2_32(Align))), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 425 | "Undefined behavior: Memory reference address is misaligned", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | void Lint::visitLoadInst(LoadInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 431 | visitMemoryReference(I, I.getPointerOperand(), |
| 432 | AA->getTypeStoreSize(I.getType()), I.getAlignment(), |
| 433 | I.getType(), MemRef::Read); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void Lint::visitStoreInst(StoreInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 437 | visitMemoryReference(I, I.getPointerOperand(), |
| 438 | AA->getTypeStoreSize(I.getOperand(0)->getType()), |
| 439 | I.getAlignment(), |
| 440 | I.getOperand(0)->getType(), MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 443 | void Lint::visitXor(BinaryOperator &I) { |
| 444 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 445 | !isa<UndefValue>(I.getOperand(1)), |
| 446 | "Undefined result: xor(undef, undef)", &I); |
| 447 | } |
| 448 | |
| 449 | void Lint::visitSub(BinaryOperator &I) { |
| 450 | Assert1(!isa<UndefValue>(I.getOperand(0)) || |
| 451 | !isa<UndefValue>(I.getOperand(1)), |
| 452 | "Undefined result: sub(undef, undef)", &I); |
| 453 | } |
| 454 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 455 | void Lint::visitLShr(BinaryOperator &I) { |
| 456 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 457 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 458 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 459 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void Lint::visitAShr(BinaryOperator &I) { |
| 463 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 464 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 465 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 466 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void Lint::visitShl(BinaryOperator &I) { |
| 470 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 471 | dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 472 | Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 473 | "Undefined result: Shift count out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 476 | static bool isZero(Value *V, TargetData *TD) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 477 | // Assume undef could be zero. |
| 478 | if (isa<UndefValue>(V)) return true; |
| 479 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 480 | unsigned BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 481 | APInt Mask = APInt::getAllOnesValue(BitWidth), |
| 482 | KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 483 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD); |
| 484 | return KnownZero.isAllOnesValue(); |
| 485 | } |
| 486 | |
| 487 | void Lint::visitSDiv(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 488 | Assert1(!isZero(I.getOperand(1), TD), |
| 489 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void Lint::visitUDiv(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 493 | Assert1(!isZero(I.getOperand(1), TD), |
| 494 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void Lint::visitSRem(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 498 | Assert1(!isZero(I.getOperand(1), TD), |
| 499 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | void Lint::visitURem(BinaryOperator &I) { |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 503 | Assert1(!isZero(I.getOperand(1), TD), |
| 504 | "Undefined behavior: Division by zero", &I); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | void Lint::visitAllocaInst(AllocaInst &I) { |
| 508 | if (isa<ConstantInt>(I.getArraySize())) |
| 509 | // This isn't undefined behavior, it's just an obvious pessimization. |
| 510 | Assert1(&I.getParent()->getParent()->getEntryBlock() == I.getParent(), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 511 | "Pessimization: Static alloca outside of entry block", &I); |
Dan Gohman | 0ce2499 | 2010-07-06 15:23:00 +0000 | [diff] [blame] | 512 | |
| 513 | // TODO: Check for an unusual size (MSB set?) |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void Lint::visitVAArgInst(VAArgInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 517 | visitMemoryReference(I, I.getOperand(0), ~0u, 0, 0, |
Dan Gohman | 5b61b38 | 2010-04-30 19:05:00 +0000 | [diff] [blame] | 518 | MemRef::Read | MemRef::Write); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void Lint::visitIndirectBrInst(IndirectBrInst &I) { |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 522 | visitMemoryReference(I, I.getAddress(), ~0u, 0, 0, MemRef::Branchee); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 525 | void Lint::visitExtractElementInst(ExtractElementInst &I) { |
| 526 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 527 | dyn_cast<ConstantInt>(findValue(I.getIndexOperand(), |
| 528 | /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 529 | Assert1(CI->getValue().ult(I.getVectorOperandType()->getNumElements()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 530 | "Undefined result: extractelement index out of range", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void Lint::visitInsertElementInst(InsertElementInst &I) { |
| 534 | if (ConstantInt *CI = |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 535 | dyn_cast<ConstantInt>(findValue(I.getOperand(2), |
| 536 | /*OffsetOk=*/false))) |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 537 | Assert1(CI->getValue().ult(I.getType()->getNumElements()), |
Dan Gohman | be02b20 | 2010-04-09 01:39:53 +0000 | [diff] [blame] | 538 | "Undefined result: insertelement index out of range", &I); |
| 539 | } |
| 540 | |
| 541 | void Lint::visitUnreachableInst(UnreachableInst &I) { |
| 542 | // This isn't undefined behavior, it's merely suspicious. |
| 543 | Assert1(&I == I.getParent()->begin() || |
| 544 | prior(BasicBlock::iterator(&I))->mayHaveSideEffects(), |
| 545 | "Unusual: unreachable immediately preceded by instruction without " |
| 546 | "side effects", &I); |
Dan Gohman | dd98c4d | 2010-04-08 23:05:57 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 549 | /// findValue - Look through bitcasts and simple memory reference patterns |
| 550 | /// to identify an equivalent, but more informative, value. If OffsetOk |
| 551 | /// is true, look through getelementptrs with non-zero offsets too. |
| 552 | /// |
| 553 | /// Most analysis passes don't require this logic, because instcombine |
| 554 | /// will simplify most of these kinds of things away. But it's a goal of |
| 555 | /// this Lint pass to be useful even on non-optimized IR. |
| 556 | Value *Lint::findValue(Value *V, bool OffsetOk) const { |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 557 | SmallPtrSet<Value *, 4> Visited; |
| 558 | return findValueImpl(V, OffsetOk, Visited); |
| 559 | } |
| 560 | |
| 561 | /// findValueImpl - Implementation helper for findValue. |
| 562 | Value *Lint::findValueImpl(Value *V, bool OffsetOk, |
| 563 | SmallPtrSet<Value *, 4> &Visited) const { |
| 564 | // Detect self-referential values. |
| 565 | if (!Visited.insert(V)) |
| 566 | return UndefValue::get(V->getType()); |
| 567 | |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 568 | // TODO: Look through sext or zext cast, when the result is known to |
| 569 | // be interpreted as signed or unsigned, respectively. |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 570 | // TODO: Look through eliminable cast pairs. |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 571 | // TODO: Look through calls with unique return values. |
| 572 | // TODO: Look through vector insert/extract/shuffle. |
| 573 | V = OffsetOk ? V->getUnderlyingObject() : V->stripPointerCasts(); |
| 574 | if (LoadInst *L = dyn_cast<LoadInst>(V)) { |
| 575 | BasicBlock::iterator BBI = L; |
| 576 | BasicBlock *BB = L->getParent(); |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 577 | SmallPtrSet<BasicBlock *, 4> VisitedBlocks; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 578 | for (;;) { |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 579 | if (!VisitedBlocks.insert(BB)) break; |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 580 | if (Value *U = FindAvailableLoadedValue(L->getPointerOperand(), |
| 581 | BB, BBI, 6, AA)) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 582 | return findValueImpl(U, OffsetOk, Visited); |
Dan Gohman | 13ec30b | 2010-05-28 17:44:00 +0000 | [diff] [blame] | 583 | if (BBI != BB->begin()) break; |
| 584 | BB = BB->getUniquePredecessor(); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 585 | if (!BB) break; |
| 586 | BBI = BB->end(); |
| 587 | } |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 588 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 589 | if (Value *W = PN->hasConstantValue(DT)) |
| 590 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 591 | } else if (CastInst *CI = dyn_cast<CastInst>(V)) { |
| 592 | if (CI->isNoopCast(TD ? TD->getIntPtrType(V->getContext()) : |
| 593 | Type::getInt64Ty(V->getContext()))) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 594 | return findValueImpl(CI->getOperand(0), OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 595 | } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) { |
| 596 | if (Value *W = FindInsertedValue(Ex->getAggregateOperand(), |
| 597 | Ex->idx_begin(), |
| 598 | Ex->idx_end())) |
| 599 | if (W != V) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 600 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | aec2a0d | 2010-05-28 21:43:57 +0000 | [diff] [blame] | 601 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 602 | // Same as above, but for ConstantExpr instead of Instruction. |
| 603 | if (Instruction::isCast(CE->getOpcode())) { |
| 604 | if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()), |
| 605 | CE->getOperand(0)->getType(), |
| 606 | CE->getType(), |
| 607 | TD ? TD->getIntPtrType(V->getContext()) : |
| 608 | Type::getInt64Ty(V->getContext()))) |
| 609 | return findValueImpl(CE->getOperand(0), OffsetOk, Visited); |
| 610 | } else if (CE->getOpcode() == Instruction::ExtractValue) { |
| 611 | const SmallVector<unsigned, 4> &Indices = CE->getIndices(); |
| 612 | if (Value *W = FindInsertedValue(CE->getOperand(0), |
| 613 | Indices.begin(), |
| 614 | Indices.end())) |
| 615 | if (W != V) |
| 616 | return findValueImpl(W, OffsetOk, Visited); |
| 617 | } |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // As a last resort, try SimplifyInstruction or constant folding. |
| 621 | if (Instruction *Inst = dyn_cast<Instruction>(V)) { |
| 622 | if (Value *W = SimplifyInstruction(Inst, TD)) |
| 623 | if (W != Inst) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 624 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 625 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 626 | if (Value *W = ConstantFoldConstantExpression(CE, TD)) |
| 627 | if (W != V) |
Dan Gohman | 17d9596 | 2010-05-28 16:45:33 +0000 | [diff] [blame] | 628 | return findValueImpl(W, OffsetOk, Visited); |
Dan Gohman | ff26d4e | 2010-05-28 16:21:24 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | return V; |
| 632 | } |
| 633 | |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 634 | //===----------------------------------------------------------------------===// |
| 635 | // Implement the public interfaces to this file... |
| 636 | //===----------------------------------------------------------------------===// |
| 637 | |
| 638 | FunctionPass *llvm::createLintPass() { |
| 639 | return new Lint(); |
| 640 | } |
| 641 | |
| 642 | /// lintFunction - Check a function for errors, printing messages on stderr. |
| 643 | /// |
| 644 | void llvm::lintFunction(const Function &f) { |
| 645 | Function &F = const_cast<Function&>(f); |
| 646 | assert(!F.isDeclaration() && "Cannot lint external functions"); |
| 647 | |
| 648 | FunctionPassManager FPM(F.getParent()); |
| 649 | Lint *V = new Lint(); |
| 650 | FPM.add(V); |
| 651 | FPM.run(F); |
| 652 | } |
| 653 | |
| 654 | /// lintModule - Check a module for errors, printing messages on stderr. |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 655 | /// |
Dan Gohman | a0f7ff3 | 2010-05-26 22:28:53 +0000 | [diff] [blame] | 656 | void llvm::lintModule(const Module &M) { |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 657 | PassManager PM; |
| 658 | Lint *V = new Lint(); |
| 659 | PM.add(V); |
| 660 | PM.run(const_cast<Module&>(M)); |
Dan Gohman | 113902e | 2010-04-08 18:47:09 +0000 | [diff] [blame] | 661 | } |