Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Support/CallSite.h" |
| 21 | using namespace llvm; |
| 22 | |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 23 | unsigned CallSite::getCallingConv() const { |
| 24 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 25 | return CI->getCallingConv(); |
| 26 | else |
| 27 | return cast<InvokeInst>(I)->getCallingConv(); |
| 28 | } |
| 29 | void CallSite::setCallingConv(unsigned CC) { |
| 30 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 31 | CI->setCallingConv(CC); |
| 32 | else |
| 33 | cast<InvokeInst>(I)->setCallingConv(CC); |
| 34 | } |
| 35 | |
| 36 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 37 | |
| 38 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 40 | // TerminatorInst Class |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | TerminatorInst::TerminatorInst(Instruction::TermOps iType, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 44 | Use *Ops, unsigned NumOps, Instruction *IB) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 45 | : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) { |
| 46 | } |
| 47 | |
| 48 | TerminatorInst::TerminatorInst(Instruction::TermOps iType, |
| 49 | Use *Ops, unsigned NumOps, BasicBlock *IAE) |
| 50 | : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) { |
| 51 | } |
| 52 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 53 | // Out of line virtual method, so the vtable, etc has a home. |
| 54 | TerminatorInst::~TerminatorInst() { |
| 55 | } |
| 56 | |
| 57 | // Out of line virtual method, so the vtable, etc has a home. |
| 58 | UnaryInstruction::~UnaryInstruction() { |
| 59 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | // PHINode Class |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
| 66 | PHINode::PHINode(const PHINode &PN) |
| 67 | : Instruction(PN.getType(), Instruction::PHI, |
| 68 | new Use[PN.getNumOperands()], PN.getNumOperands()), |
| 69 | ReservedSpace(PN.getNumOperands()) { |
| 70 | Use *OL = OperandList; |
| 71 | for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { |
| 72 | OL[i].init(PN.getOperand(i), this); |
| 73 | OL[i+1].init(PN.getOperand(i+1), this); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | PHINode::~PHINode() { |
| 78 | delete [] OperandList; |
| 79 | } |
| 80 | |
| 81 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 82 | // predecessor basic block is deleted. |
| 83 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
| 84 | unsigned NumOps = getNumOperands(); |
| 85 | Use *OL = OperandList; |
| 86 | assert(Idx*2 < NumOps && "BB not in PHI node!"); |
| 87 | Value *Removed = OL[Idx*2]; |
| 88 | |
| 89 | // Move everything after this operand down. |
| 90 | // |
| 91 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 92 | // client might not expect this to happen. The code as it is thrashes the |
| 93 | // use/def lists, which is kinda lame. |
| 94 | for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) { |
| 95 | OL[i-2] = OL[i]; |
| 96 | OL[i-2+1] = OL[i+1]; |
| 97 | } |
| 98 | |
| 99 | // Nuke the last value. |
| 100 | OL[NumOps-2].set(0); |
| 101 | OL[NumOps-2+1].set(0); |
| 102 | NumOperands = NumOps-2; |
| 103 | |
| 104 | // If the PHI node is dead, because it has zero entries, nuke it now. |
| 105 | if (NumOps == 2 && DeletePHIIfEmpty) { |
| 106 | // If anyone is using this PHI, make them use a dummy value instead... |
| 107 | replaceAllUsesWith(UndefValue::get(getType())); |
| 108 | eraseFromParent(); |
| 109 | } |
| 110 | return Removed; |
| 111 | } |
| 112 | |
| 113 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 114 | /// list according to the following behavior: |
| 115 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 116 | /// of operation. This grows the number of ops by 1.5 times. |
| 117 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 118 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 119 | /// |
| 120 | void PHINode::resizeOperands(unsigned NumOps) { |
| 121 | if (NumOps == 0) { |
| 122 | NumOps = (getNumOperands())*3/2; |
| 123 | if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. |
| 124 | } else if (NumOps*2 > NumOperands) { |
| 125 | // No resize needed. |
| 126 | if (ReservedSpace >= NumOps) return; |
| 127 | } else if (NumOps == NumOperands) { |
| 128 | if (ReservedSpace == NumOps) return; |
| 129 | } else { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 130 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ReservedSpace = NumOps; |
| 134 | Use *NewOps = new Use[NumOps]; |
| 135 | Use *OldOps = OperandList; |
| 136 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 137 | NewOps[i].init(OldOps[i], this); |
| 138 | OldOps[i].set(0); |
| 139 | } |
| 140 | delete [] OldOps; |
| 141 | OperandList = NewOps; |
| 142 | } |
| 143 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 144 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 145 | /// value, return the value, otherwise return null. |
| 146 | /// |
Chris Lattner | 1d8b248 | 2005-08-05 00:49:06 +0000 | [diff] [blame] | 147 | Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 148 | // If the PHI node only has one incoming value, eliminate the PHI node... |
| 149 | if (getNumIncomingValues() == 1) |
Chris Lattner | 6e709c1 | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 150 | if (getIncomingValue(0) != this) // not X = phi X |
| 151 | return getIncomingValue(0); |
| 152 | else |
| 153 | return UndefValue::get(getType()); // Self cycle is dead. |
| 154 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 155 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 156 | // the PHI node with the incoming value. |
| 157 | // |
| 158 | Value *InVal = 0; |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 159 | bool HasUndefInput = false; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 160 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 161 | if (isa<UndefValue>(getIncomingValue(i))) |
| 162 | HasUndefInput = true; |
| 163 | else if (getIncomingValue(i) != this) // Not the PHI node itself... |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 164 | if (InVal && getIncomingValue(i) != InVal) |
| 165 | return 0; // Not the same, bail out. |
| 166 | else |
| 167 | InVal = getIncomingValue(i); |
| 168 | |
| 169 | // The only case that could cause InVal to be null is if we have a PHI node |
| 170 | // that only has entries for itself. In this case, there is no entry into the |
| 171 | // loop, so kill the PHI. |
| 172 | // |
| 173 | if (InVal == 0) InVal = UndefValue::get(getType()); |
| 174 | |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 175 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 176 | // instruction, we cannot always return X as the result of the PHI node. Only |
| 177 | // do this if X is not an instruction (thus it must dominate the PHI block), |
| 178 | // or if the client is prepared to deal with this possibility. |
| 179 | if (HasUndefInput && !AllowNonDominatingInstruction) |
| 180 | if (Instruction *IV = dyn_cast<Instruction>(InVal)) |
| 181 | // If it's in the entry block, it dominates everything. |
Chris Lattner | 37774af | 2005-08-05 01:03:27 +0000 | [diff] [blame] | 182 | if (IV->getParent() != &IV->getParent()->getParent()->front() || |
| 183 | isa<InvokeInst>(IV)) |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 184 | return 0; // Cannot guarantee that InVal dominates this PHINode. |
| 185 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 186 | // All of the incoming values are the same, return the value now. |
| 187 | return InVal; |
| 188 | } |
| 189 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 190 | |
| 191 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 192 | // CallInst Implementation |
| 193 | //===----------------------------------------------------------------------===// |
| 194 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 195 | CallInst::~CallInst() { |
| 196 | delete [] OperandList; |
| 197 | } |
| 198 | |
| 199 | void CallInst::init(Value *Func, const std::vector<Value*> &Params) { |
| 200 | NumOperands = Params.size()+1; |
| 201 | Use *OL = OperandList = new Use[Params.size()+1]; |
| 202 | OL[0].init(Func, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 203 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 204 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 205 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 206 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 207 | assert((Params.size() == FTy->getNumParams() || |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 208 | (FTy->isVarArg() && Params.size() > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 209 | "Calling a function with bad signature!"); |
| 210 | for (unsigned i = 0, e = Params.size(); i != e; ++i) { |
| 211 | assert((i >= FTy->getNumParams() || |
| 212 | FTy->getParamType(i) == Params[i]->getType()) && |
| 213 | "Calling a function with a bad signature!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 214 | OL[i+1].init(Params[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 215 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 218 | void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { |
| 219 | NumOperands = 3; |
| 220 | Use *OL = OperandList = new Use[3]; |
| 221 | OL[0].init(Func, this); |
| 222 | OL[1].init(Actual1, this); |
| 223 | OL[2].init(Actual2, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 224 | |
| 225 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 226 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 227 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 228 | assert((FTy->getNumParams() == 2 || |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 229 | (FTy->isVarArg() && FTy->getNumParams() < 2)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 230 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 231 | assert((0 >= FTy->getNumParams() || |
| 232 | FTy->getParamType(0) == Actual1->getType()) && |
| 233 | "Calling a function with a bad signature!"); |
| 234 | assert((1 >= FTy->getNumParams() || |
| 235 | FTy->getParamType(1) == Actual2->getType()) && |
| 236 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 239 | void CallInst::init(Value *Func, Value *Actual) { |
| 240 | NumOperands = 2; |
| 241 | Use *OL = OperandList = new Use[2]; |
| 242 | OL[0].init(Func, this); |
| 243 | OL[1].init(Actual, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 244 | |
| 245 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 246 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 247 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 248 | assert((FTy->getNumParams() == 1 || |
| 249 | (FTy->isVarArg() && FTy->getNumParams() == 0)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 250 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 251 | assert((0 == FTy->getNumParams() || |
| 252 | FTy->getParamType(0) == Actual->getType()) && |
| 253 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 256 | void CallInst::init(Value *Func) { |
| 257 | NumOperands = 1; |
| 258 | Use *OL = OperandList = new Use[1]; |
| 259 | OL[0].init(Func, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 260 | |
| 261 | const FunctionType *MTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 262 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 263 | |
| 264 | assert(MTy->getNumParams() == 0 && "Calling a function with bad signature"); |
| 265 | } |
| 266 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 267 | CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, |
| 268 | const std::string &Name, Instruction *InsertBefore) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 269 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 270 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 271 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 272 | init(Func, Params); |
| 273 | } |
| 274 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 275 | CallInst::CallInst(Value *Func, const std::vector<Value*> &Params, |
| 276 | const std::string &Name, BasicBlock *InsertAtEnd) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 277 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 278 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 279 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 280 | init(Func, Params); |
| 281 | } |
| 282 | |
| 283 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 284 | const std::string &Name, Instruction *InsertBefore) |
| 285 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 286 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 287 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 288 | init(Func, Actual1, Actual2); |
| 289 | } |
| 290 | |
| 291 | CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, |
| 292 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 293 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 294 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 295 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 296 | init(Func, Actual1, Actual2); |
| 297 | } |
| 298 | |
| 299 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 300 | Instruction *InsertBefore) |
| 301 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 302 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 303 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 304 | init(Func, Actual); |
| 305 | } |
| 306 | |
| 307 | CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, |
| 308 | BasicBlock *InsertAtEnd) |
| 309 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 310 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 311 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 312 | init(Func, Actual); |
| 313 | } |
| 314 | |
| 315 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 316 | Instruction *InsertBefore) |
| 317 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 318 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 319 | Instruction::Call, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 320 | init(Func); |
| 321 | } |
| 322 | |
| 323 | CallInst::CallInst(Value *Func, const std::string &Name, |
| 324 | BasicBlock *InsertAtEnd) |
| 325 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 326 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 327 | Instruction::Call, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 328 | init(Func); |
| 329 | } |
| 330 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 331 | CallInst::CallInst(const CallInst &CI) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 332 | : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()], |
| 333 | CI.getNumOperands()) { |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 334 | SubclassData = CI.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 335 | Use *OL = OperandList; |
| 336 | Use *InOL = CI.OperandList; |
| 337 | for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) |
| 338 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 341 | |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | // InvokeInst Implementation |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 346 | InvokeInst::~InvokeInst() { |
| 347 | delete [] OperandList; |
| 348 | } |
| 349 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 350 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 351 | const std::vector<Value*> &Params) { |
| 352 | NumOperands = 3+Params.size(); |
| 353 | Use *OL = OperandList = new Use[3+Params.size()]; |
| 354 | OL[0].init(Fn, this); |
| 355 | OL[1].init(IfNormal, this); |
| 356 | OL[2].init(IfException, this); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 357 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 358 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 359 | |
| 360 | assert((Params.size() == FTy->getNumParams()) || |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 361 | (FTy->isVarArg() && Params.size() > FTy->getNumParams()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 362 | "Calling a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 364 | for (unsigned i = 0, e = Params.size(); i != e; i++) { |
| 365 | assert((i >= FTy->getNumParams() || |
| 366 | FTy->getParamType(i) == Params[i]->getType()) && |
| 367 | "Invoking a function with a bad signature!"); |
| 368 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 369 | OL[i+3].init(Params[i], this); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 370 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 374 | BasicBlock *IfException, |
| 375 | const std::vector<Value*> &Params, |
| 376 | const std::string &Name, Instruction *InsertBefore) |
| 377 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 378 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 379 | Instruction::Invoke, 0, 0, Name, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 380 | init(Fn, IfNormal, IfException, Params); |
| 381 | } |
| 382 | |
| 383 | InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, |
| 384 | BasicBlock *IfException, |
| 385 | const std::vector<Value*> &Params, |
| 386 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 387 | : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType()) |
| 388 | ->getElementType())->getReturnType(), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 389 | Instruction::Invoke, 0, 0, Name, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 390 | init(Fn, IfNormal, IfException, Params); |
| 391 | } |
| 392 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 393 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 394 | : TerminatorInst(II.getType(), Instruction::Invoke, |
| 395 | new Use[II.getNumOperands()], II.getNumOperands()) { |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 396 | SubclassData = II.SubclassData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 397 | Use *OL = OperandList, *InOL = II.OperandList; |
| 398 | for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) |
| 399 | OL[i].init(InOL[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 402 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 403 | return getSuccessor(idx); |
| 404 | } |
| 405 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 406 | return getNumSuccessors(); |
| 407 | } |
| 408 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 409 | return setSuccessor(idx, B); |
| 410 | } |
| 411 | |
| 412 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 413 | //===----------------------------------------------------------------------===// |
| 414 | // ReturnInst Implementation |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 417 | void ReturnInst::init(Value *retVal) { |
| 418 | if (retVal && retVal->getType() != Type::VoidTy) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 419 | assert(!isa<BasicBlock>(retVal) && |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 420 | "Cannot return basic block. Probably using the incorrect ctor"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 421 | NumOperands = 1; |
| 422 | RetVal.init(retVal, this); |
Alkis Evlogimenos | 531e901 | 2004-11-17 21:02:25 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 426 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 427 | return getNumSuccessors(); |
| 428 | } |
| 429 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 430 | // Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 431 | // emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 432 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 433 | assert(0 && "ReturnInst has no successors!"); |
| 434 | } |
| 435 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 436 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
| 437 | assert(0 && "ReturnInst has no successors!"); |
| 438 | abort(); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 443 | //===----------------------------------------------------------------------===// |
| 444 | // UnwindInst Implementation |
| 445 | //===----------------------------------------------------------------------===// |
| 446 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 447 | unsigned UnwindInst::getNumSuccessorsV() const { |
| 448 | return getNumSuccessors(); |
| 449 | } |
| 450 | |
| 451 | void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 452 | assert(0 && "UnwindInst has no successors!"); |
| 453 | } |
| 454 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 455 | BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { |
| 456 | assert(0 && "UnwindInst has no successors!"); |
| 457 | abort(); |
| 458 | return 0; |
| 459 | } |
| 460 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 461 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 462 | // UnreachableInst Implementation |
| 463 | //===----------------------------------------------------------------------===// |
| 464 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 465 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 466 | return getNumSuccessors(); |
| 467 | } |
| 468 | |
| 469 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 470 | assert(0 && "UnwindInst has no successors!"); |
| 471 | } |
| 472 | |
| 473 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
| 474 | assert(0 && "UnwindInst has no successors!"); |
| 475 | abort(); |
| 476 | return 0; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 480 | // BranchInst Implementation |
| 481 | //===----------------------------------------------------------------------===// |
| 482 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 483 | void BranchInst::AssertOK() { |
| 484 | if (isConditional()) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 485 | assert(getCondition()->getType() == Type::BoolTy && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 486 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 489 | BranchInst::BranchInst(const BranchInst &BI) : |
| 490 | TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) { |
| 491 | OperandList[0].init(BI.getOperand(0), this); |
| 492 | if (BI.getNumOperands() != 1) { |
| 493 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
| 494 | OperandList[1].init(BI.getOperand(1), this); |
| 495 | OperandList[2].init(BI.getOperand(2), this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 499 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 500 | return getSuccessor(idx); |
| 501 | } |
| 502 | unsigned BranchInst::getNumSuccessorsV() const { |
| 503 | return getNumSuccessors(); |
| 504 | } |
| 505 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 506 | setSuccessor(idx, B); |
| 507 | } |
| 508 | |
| 509 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 510 | //===----------------------------------------------------------------------===// |
| 511 | // AllocationInst Implementation |
| 512 | //===----------------------------------------------------------------------===// |
| 513 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 514 | static Value *getAISize(Value *Amt) { |
| 515 | if (!Amt) |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 516 | Amt = ConstantInt::get(Type::UIntTy, 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 517 | else { |
| 518 | assert(!isa<BasicBlock>(Amt) && |
| 519 | "Passed basic block into allocation size parameter! Ue other ctor"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 520 | assert(Amt->getType() == Type::UIntTy && |
| 521 | "Malloc/Allocation array size != UIntTy!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 522 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 523 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 526 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 527 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 528 | Instruction *InsertBefore) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 529 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 530 | Name, InsertBefore), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 531 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 532 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 535 | AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 536 | unsigned Align, const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 537 | BasicBlock *InsertAtEnd) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 538 | : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 539 | Name, InsertAtEnd), Alignment(Align) { |
Chris Lattner | 79b8c79 | 2005-11-05 21:57:54 +0000 | [diff] [blame] | 540 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 541 | assert(Ty != Type::VoidTy && "Cannot allocate void!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Chris Lattner | 1c12a88 | 2006-06-21 16:53:47 +0000 | [diff] [blame] | 544 | // Out of line virtual method, so the vtable, etc has a home. |
| 545 | AllocationInst::~AllocationInst() { |
| 546 | } |
| 547 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 548 | bool AllocationInst::isArrayAllocation() const { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 549 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0))) |
| 550 | return CUI->getZExtValue() != 1; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 551 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | const Type *AllocationInst::getAllocatedType() const { |
| 555 | return getType()->getElementType(); |
| 556 | } |
| 557 | |
| 558 | AllocaInst::AllocaInst(const AllocaInst &AI) |
| 559 | : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 560 | Instruction::Alloca, AI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | MallocInst::MallocInst(const MallocInst &MI) |
| 564 | : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 565 | Instruction::Malloc, MI.getAlignment()) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | //===----------------------------------------------------------------------===// |
| 569 | // FreeInst Implementation |
| 570 | //===----------------------------------------------------------------------===// |
| 571 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 572 | void FreeInst::AssertOK() { |
| 573 | assert(isa<PointerType>(getOperand(0)->getType()) && |
| 574 | "Can not free something of nonpointer type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 578 | : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) { |
| 579 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 583 | : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) { |
| 584 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
| 588 | //===----------------------------------------------------------------------===// |
| 589 | // LoadInst Implementation |
| 590 | //===----------------------------------------------------------------------===// |
| 591 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 592 | void LoadInst::AssertOK() { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 593 | assert(isa<PointerType>(getOperand(0)->getType()) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 594 | "Ptr must have pointer type."); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 598 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 599 | Load, Ptr, Name, InsertBef) { |
| 600 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 601 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 605 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 606 | Load, Ptr, Name, InsertAE) { |
| 607 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 608 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 612 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 613 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 614 | Load, Ptr, Name, InsertBef) { |
| 615 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 616 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, |
| 620 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 621 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 622 | Load, Ptr, Name, InsertAE) { |
| 623 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 624 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
| 628 | //===----------------------------------------------------------------------===// |
| 629 | // StoreInst Implementation |
| 630 | //===----------------------------------------------------------------------===// |
| 631 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 632 | void StoreInst::AssertOK() { |
| 633 | assert(isa<PointerType>(getOperand(1)->getType()) && |
| 634 | "Ptr must have pointer type!"); |
| 635 | assert(getOperand(0)->getType() == |
| 636 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 637 | && "Ptr must be a pointer to Val type!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 638 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 639 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 640 | |
| 641 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 642 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 643 | Ops[0].init(val, this); |
| 644 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 645 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 646 | AssertOK(); |
| 647 | } |
| 648 | |
| 649 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 650 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 651 | Ops[0].init(val, this); |
| 652 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 653 | setVolatile(false); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 654 | AssertOK(); |
| 655 | } |
| 656 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 657 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 658 | Instruction *InsertBefore) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 659 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 660 | Ops[0].init(val, this); |
| 661 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 662 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 663 | AssertOK(); |
| 664 | } |
| 665 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 666 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 667 | BasicBlock *InsertAtEnd) |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 668 | : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 669 | Ops[0].init(val, this); |
| 670 | Ops[1].init(addr, this); |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 671 | setVolatile(isVolatile); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 672 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | //===----------------------------------------------------------------------===// |
| 676 | // GetElementPtrInst Implementation |
| 677 | //===----------------------------------------------------------------------===// |
| 678 | |
| 679 | // checkType - Simple wrapper function to give a better assertion failure |
| 680 | // message on bad indexes for a gep instruction. |
| 681 | // |
| 682 | static inline const Type *checkType(const Type *Ty) { |
Chris Lattner | 47a6e63 | 2006-05-14 18:34:36 +0000 | [diff] [blame] | 683 | assert(Ty && "Invalid GetElementPtrInst indices for type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 684 | return Ty; |
| 685 | } |
| 686 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 687 | void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) { |
| 688 | NumOperands = 1+Idx.size(); |
| 689 | Use *OL = OperandList = new Use[NumOperands]; |
| 690 | OL[0].init(Ptr, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 691 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 692 | for (unsigned i = 0, e = Idx.size(); i != e; ++i) |
| 693 | OL[i+1].init(Idx[i], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 697 | NumOperands = 3; |
| 698 | Use *OL = OperandList = new Use[3]; |
| 699 | OL[0].init(Ptr, this); |
| 700 | OL[1].init(Idx0, this); |
| 701 | OL[2].init(Idx1, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 704 | void GetElementPtrInst::init(Value *Ptr, Value *Idx) { |
| 705 | NumOperands = 2; |
| 706 | Use *OL = OperandList = new Use[2]; |
| 707 | OL[0].init(Ptr, this); |
| 708 | OL[1].init(Idx, this); |
| 709 | } |
| 710 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 711 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 712 | const std::string &Name, Instruction *InBe) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 713 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 714 | Idx, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 715 | GetElementPtr, 0, 0, Name, InBe) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 716 | init(Ptr, Idx); |
| 717 | } |
| 718 | |
| 719 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 720 | const std::string &Name, BasicBlock *IAE) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 721 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 722 | Idx, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 723 | GetElementPtr, 0, 0, Name, IAE) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 724 | init(Ptr, Idx); |
| 725 | } |
| 726 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 727 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 728 | const std::string &Name, Instruction *InBe) |
| 729 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 730 | GetElementPtr, 0, 0, Name, InBe) { |
| 731 | init(Ptr, Idx); |
| 732 | } |
| 733 | |
| 734 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 735 | const std::string &Name, BasicBlock *IAE) |
| 736 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 737 | GetElementPtr, 0, 0, Name, IAE) { |
| 738 | init(Ptr, Idx); |
| 739 | } |
| 740 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 741 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
| 742 | const std::string &Name, Instruction *InBe) |
| 743 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 744 | Idx0, Idx1, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 745 | GetElementPtr, 0, 0, Name, InBe) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 746 | init(Ptr, Idx0, Idx1); |
| 747 | } |
| 748 | |
| 749 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 750 | const std::string &Name, BasicBlock *IAE) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 751 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 752 | Idx0, Idx1, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 753 | GetElementPtr, 0, 0, Name, IAE) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 754 | init(Ptr, Idx0, Idx1); |
| 755 | } |
| 756 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 757 | GetElementPtrInst::~GetElementPtrInst() { |
| 758 | delete[] OperandList; |
| 759 | } |
| 760 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 761 | // getIndexedType - Returns the type of the element that would be loaded with |
| 762 | // a load instruction with the specified parameters. |
| 763 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 764 | // A null type is returned if the indices are invalid for the specified |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 765 | // pointer type. |
| 766 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 767 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 768 | const std::vector<Value*> &Idx, |
| 769 | bool AllowCompositeLeaf) { |
| 770 | if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type! |
| 771 | |
| 772 | // Handle the special case of the empty set index set... |
| 773 | if (Idx.empty()) |
| 774 | if (AllowCompositeLeaf || |
| 775 | cast<PointerType>(Ptr)->getElementType()->isFirstClassType()) |
| 776 | return cast<PointerType>(Ptr)->getElementType(); |
| 777 | else |
| 778 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 779 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 780 | unsigned CurIdx = 0; |
| 781 | while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
| 782 | if (Idx.size() == CurIdx) { |
| 783 | if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; |
| 784 | return 0; // Can't load a whole structure or array!?!? |
| 785 | } |
| 786 | |
| 787 | Value *Index = Idx[CurIdx++]; |
| 788 | if (isa<PointerType>(CT) && CurIdx != 1) |
| 789 | return 0; // Can only index into pointer types at the first index! |
| 790 | if (!CT->indexValid(Index)) return 0; |
| 791 | Ptr = CT->getTypeAtIndex(Index); |
| 792 | |
| 793 | // If the new type forwards to another type, then it is in the middle |
| 794 | // of being refined to another type (and hence, may have dropped all |
| 795 | // references to what it was using before). So, use the new forwarded |
| 796 | // type. |
| 797 | if (const Type * Ty = Ptr->getForwardedType()) { |
| 798 | Ptr = Ty; |
| 799 | } |
| 800 | } |
| 801 | return CurIdx == Idx.size() ? Ptr : 0; |
| 802 | } |
| 803 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 804 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 805 | Value *Idx0, Value *Idx1, |
| 806 | bool AllowCompositeLeaf) { |
| 807 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 808 | if (!PTy) return 0; // Type isn't a pointer type! |
| 809 | |
| 810 | // Check the pointer index. |
| 811 | if (!PTy->indexValid(Idx0)) return 0; |
| 812 | |
| 813 | const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType()); |
| 814 | if (!CT || !CT->indexValid(Idx1)) return 0; |
| 815 | |
| 816 | const Type *ElTy = CT->getTypeAtIndex(Idx1); |
| 817 | if (AllowCompositeLeaf || ElTy->isFirstClassType()) |
| 818 | return ElTy; |
| 819 | return 0; |
| 820 | } |
| 821 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 822 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 823 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 824 | if (!PTy) return 0; // Type isn't a pointer type! |
| 825 | |
| 826 | // Check the pointer index. |
| 827 | if (!PTy->indexValid(Idx)) return 0; |
| 828 | |
Chris Lattner | c223333 | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 829 | return PTy->getElementType(); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 832 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 833 | // ExtractElementInst Implementation |
| 834 | //===----------------------------------------------------------------------===// |
| 835 | |
| 836 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 837 | const std::string &Name, |
| 838 | Instruction *InsertBef) |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 839 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 840 | ExtractElement, Ops, 2, Name, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 841 | assert(isValidOperands(Val, Index) && |
| 842 | "Invalid extractelement instruction operands!"); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 843 | Ops[0].init(Val, this); |
| 844 | Ops[1].init(Index, this); |
| 845 | } |
| 846 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 847 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 848 | const std::string &Name, |
| 849 | Instruction *InsertBef) |
| 850 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 851 | ExtractElement, Ops, 2, Name, InsertBef) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 852 | Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 853 | assert(isValidOperands(Val, Index) && |
| 854 | "Invalid extractelement instruction operands!"); |
| 855 | Ops[0].init(Val, this); |
| 856 | Ops[1].init(Index, this); |
| 857 | } |
| 858 | |
| 859 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 860 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 861 | const std::string &Name, |
| 862 | BasicBlock *InsertAE) |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 863 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 864 | ExtractElement, Ops, 2, Name, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 865 | assert(isValidOperands(Val, Index) && |
| 866 | "Invalid extractelement instruction operands!"); |
| 867 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 868 | Ops[0].init(Val, this); |
| 869 | Ops[1].init(Index, this); |
| 870 | } |
| 871 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 872 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 873 | const std::string &Name, |
| 874 | BasicBlock *InsertAE) |
| 875 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 876 | ExtractElement, Ops, 2, Name, InsertAE) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 877 | Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 878 | assert(isValidOperands(Val, Index) && |
| 879 | "Invalid extractelement instruction operands!"); |
| 880 | |
| 881 | Ops[0].init(Val, this); |
| 882 | Ops[1].init(Index, this); |
| 883 | } |
| 884 | |
| 885 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 886 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
| 887 | if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::UIntTy) |
| 888 | return false; |
| 889 | return true; |
| 890 | } |
| 891 | |
| 892 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 893 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 894 | // InsertElementInst Implementation |
| 895 | //===----------------------------------------------------------------------===// |
| 896 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 897 | InsertElementInst::InsertElementInst(const InsertElementInst &IE) |
| 898 | : Instruction(IE.getType(), InsertElement, Ops, 3) { |
| 899 | Ops[0].init(IE.Ops[0], this); |
| 900 | Ops[1].init(IE.Ops[1], this); |
| 901 | Ops[2].init(IE.Ops[2], this); |
| 902 | } |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 903 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 904 | const std::string &Name, |
| 905 | Instruction *InsertBef) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 906 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { |
| 907 | assert(isValidOperands(Vec, Elt, Index) && |
| 908 | "Invalid insertelement instruction operands!"); |
| 909 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 910 | Ops[1].init(Elt, this); |
| 911 | Ops[2].init(Index, this); |
| 912 | } |
| 913 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 914 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 915 | const std::string &Name, |
| 916 | Instruction *InsertBef) |
| 917 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 918 | Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 919 | assert(isValidOperands(Vec, Elt, Index) && |
| 920 | "Invalid insertelement instruction operands!"); |
| 921 | Ops[0].init(Vec, this); |
| 922 | Ops[1].init(Elt, this); |
| 923 | Ops[2].init(Index, this); |
| 924 | } |
| 925 | |
| 926 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 927 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 928 | const std::string &Name, |
| 929 | BasicBlock *InsertAE) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 930 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { |
| 931 | assert(isValidOperands(Vec, Elt, Index) && |
| 932 | "Invalid insertelement instruction operands!"); |
| 933 | |
| 934 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 935 | Ops[1].init(Elt, this); |
| 936 | Ops[2].init(Index, this); |
| 937 | } |
| 938 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 939 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 940 | const std::string &Name, |
| 941 | BasicBlock *InsertAE) |
| 942 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 943 | Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 944 | assert(isValidOperands(Vec, Elt, Index) && |
| 945 | "Invalid insertelement instruction operands!"); |
| 946 | |
| 947 | Ops[0].init(Vec, this); |
| 948 | Ops[1].init(Elt, this); |
| 949 | Ops[2].init(Index, this); |
| 950 | } |
| 951 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 952 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 953 | const Value *Index) { |
| 954 | if (!isa<PackedType>(Vec->getType())) |
| 955 | return false; // First operand of insertelement must be packed type. |
| 956 | |
| 957 | if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType()) |
| 958 | return false;// Second operand of insertelement must be packed element type. |
| 959 | |
| 960 | if (Index->getType() != Type::UIntTy) |
| 961 | return false; // Third operand of insertelement must be uint. |
| 962 | return true; |
| 963 | } |
| 964 | |
| 965 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 966 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 967 | // ShuffleVectorInst Implementation |
| 968 | //===----------------------------------------------------------------------===// |
| 969 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 970 | ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) |
| 971 | : Instruction(SV.getType(), ShuffleVector, Ops, 3) { |
| 972 | Ops[0].init(SV.Ops[0], this); |
| 973 | Ops[1].init(SV.Ops[1], this); |
| 974 | Ops[2].init(SV.Ops[2], this); |
| 975 | } |
| 976 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 977 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 978 | const std::string &Name, |
| 979 | Instruction *InsertBefore) |
| 980 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) { |
| 981 | assert(isValidOperands(V1, V2, Mask) && |
| 982 | "Invalid shuffle vector instruction operands!"); |
| 983 | Ops[0].init(V1, this); |
| 984 | Ops[1].init(V2, this); |
| 985 | Ops[2].init(Mask, this); |
| 986 | } |
| 987 | |
| 988 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 989 | const std::string &Name, |
| 990 | BasicBlock *InsertAtEnd) |
| 991 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) { |
| 992 | assert(isValidOperands(V1, V2, Mask) && |
| 993 | "Invalid shuffle vector instruction operands!"); |
| 994 | |
| 995 | Ops[0].init(V1, this); |
| 996 | Ops[1].init(V2, this); |
| 997 | Ops[2].init(Mask, this); |
| 998 | } |
| 999 | |
| 1000 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
| 1001 | const Value *Mask) { |
| 1002 | if (!isa<PackedType>(V1->getType())) return false; |
| 1003 | if (V1->getType() != V2->getType()) return false; |
| 1004 | if (!isa<PackedType>(Mask->getType()) || |
| 1005 | cast<PackedType>(Mask->getType())->getElementType() != Type::UIntTy || |
| 1006 | cast<PackedType>(Mask->getType())->getNumElements() != |
| 1007 | cast<PackedType>(V1->getType())->getNumElements()) |
| 1008 | return false; |
| 1009 | return true; |
| 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1014 | // BinaryOperator Class |
| 1015 | //===----------------------------------------------------------------------===// |
| 1016 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1017 | void BinaryOperator::init(BinaryOps iType) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1018 | { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1019 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
| 1020 | assert(LHS->getType() == RHS->getType() && |
| 1021 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1022 | #ifndef NDEBUG |
| 1023 | switch (iType) { |
| 1024 | case Add: case Sub: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1025 | case Mul: |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1026 | case Rem: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1027 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1028 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1029 | assert((getType()->isInteger() || getType()->isFloatingPoint() || |
| 1030 | isa<PackedType>(getType())) && |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1031 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1032 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1033 | case UDiv: |
| 1034 | case SDiv: |
| 1035 | assert(getType() == LHS->getType() && |
| 1036 | "Arithmetic operation should return same type as operands!"); |
| 1037 | assert((getType()->isInteger() || (isa<PackedType>(getType()) && |
| 1038 | cast<PackedType>(getType())->getElementType()->isInteger())) && |
| 1039 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1040 | break; |
| 1041 | case FDiv: |
| 1042 | assert(getType() == LHS->getType() && |
| 1043 | "Arithmetic operation should return same type as operands!"); |
| 1044 | assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) && |
| 1045 | cast<PackedType>(getType())->getElementType()->isFloatingPoint())) |
| 1046 | && "Incorrect operand type (not floating point) for FDIV"); |
| 1047 | break; |
| 1048 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1049 | case And: case Or: |
| 1050 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1051 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1052 | "Logical operation should return same type as operands!"); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1053 | assert((getType()->isIntegral() || |
| 1054 | (isa<PackedType>(getType()) && |
| 1055 | cast<PackedType>(getType())->getElementType()->isIntegral())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1056 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1057 | break; |
| 1058 | case SetLT: case SetGT: case SetLE: |
| 1059 | case SetGE: case SetEQ: case SetNE: |
| 1060 | assert(getType() == Type::BoolTy && "Setcc must return bool!"); |
| 1061 | default: |
| 1062 | break; |
| 1063 | } |
| 1064 | #endif |
| 1065 | } |
| 1066 | |
| 1067 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1068 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1069 | Instruction *InsertBefore) { |
| 1070 | assert(S1->getType() == S2->getType() && |
| 1071 | "Cannot create binary operator with two operands of differing type!"); |
| 1072 | switch (Op) { |
| 1073 | // Binary comparison operators... |
| 1074 | case SetLT: case SetGT: case SetLE: |
| 1075 | case SetGE: case SetEQ: case SetNE: |
| 1076 | return new SetCondInst(Op, S1, S2, Name, InsertBefore); |
| 1077 | |
| 1078 | default: |
| 1079 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1084 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1085 | BasicBlock *InsertAtEnd) { |
| 1086 | BinaryOperator *Res = create(Op, S1, S2, Name); |
| 1087 | InsertAtEnd->getInstList().push_back(Res); |
| 1088 | return Res; |
| 1089 | } |
| 1090 | |
| 1091 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1092 | Instruction *InsertBefore) { |
| 1093 | if (!Op->getType()->isFloatingPoint()) |
| 1094 | return new BinaryOperator(Instruction::Sub, |
| 1095 | Constant::getNullValue(Op->getType()), Op, |
| 1096 | Op->getType(), Name, InsertBefore); |
| 1097 | else |
| 1098 | return new BinaryOperator(Instruction::Sub, |
| 1099 | ConstantFP::get(Op->getType(), -0.0), Op, |
| 1100 | Op->getType(), Name, InsertBefore); |
| 1101 | } |
| 1102 | |
| 1103 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1104 | BasicBlock *InsertAtEnd) { |
| 1105 | if (!Op->getType()->isFloatingPoint()) |
| 1106 | return new BinaryOperator(Instruction::Sub, |
| 1107 | Constant::getNullValue(Op->getType()), Op, |
| 1108 | Op->getType(), Name, InsertAtEnd); |
| 1109 | else |
| 1110 | return new BinaryOperator(Instruction::Sub, |
| 1111 | ConstantFP::get(Op->getType(), -0.0), Op, |
| 1112 | Op->getType(), Name, InsertAtEnd); |
| 1113 | } |
| 1114 | |
| 1115 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1116 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1117 | Constant *C; |
| 1118 | if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) { |
| 1119 | C = ConstantIntegral::getAllOnesValue(PTy->getElementType()); |
| 1120 | C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C)); |
| 1121 | } else { |
| 1122 | C = ConstantIntegral::getAllOnesValue(Op->getType()); |
| 1123 | } |
| 1124 | |
| 1125 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1126 | Op->getType(), Name, InsertBefore); |
| 1127 | } |
| 1128 | |
| 1129 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1130 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1131 | Constant *AllOnes; |
| 1132 | if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) { |
| 1133 | // Create a vector of all ones values. |
| 1134 | Constant *Elt = ConstantIntegral::getAllOnesValue(PTy->getElementType()); |
| 1135 | AllOnes = |
| 1136 | ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt)); |
| 1137 | } else { |
| 1138 | AllOnes = ConstantIntegral::getAllOnesValue(Op->getType()); |
| 1139 | } |
| 1140 | |
| 1141 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1142 | Op->getType(), Name, InsertAtEnd); |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | // isConstantAllOnes - Helper function for several functions below |
| 1147 | static inline bool isConstantAllOnes(const Value *V) { |
| 1148 | return isa<ConstantIntegral>(V) &&cast<ConstantIntegral>(V)->isAllOnesValue(); |
| 1149 | } |
| 1150 | |
| 1151 | bool BinaryOperator::isNeg(const Value *V) { |
| 1152 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1153 | if (Bop->getOpcode() == Instruction::Sub) |
| 1154 | if (!V->getType()->isFloatingPoint()) |
| 1155 | return Bop->getOperand(0) == Constant::getNullValue(Bop->getType()); |
| 1156 | else |
| 1157 | return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0); |
| 1158 | return false; |
| 1159 | } |
| 1160 | |
| 1161 | bool BinaryOperator::isNot(const Value *V) { |
| 1162 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1163 | return (Bop->getOpcode() == Instruction::Xor && |
| 1164 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1165 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1169 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
| 1170 | assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!"); |
| 1171 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1174 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1175 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1178 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1179 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1180 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1181 | Value *Op0 = BO->getOperand(0); |
| 1182 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1183 | if (isConstantAllOnes(Op0)) return Op1; |
| 1184 | |
| 1185 | assert(isConstantAllOnes(Op1)); |
| 1186 | return Op0; |
| 1187 | } |
| 1188 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1189 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1190 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | // swapOperands - Exchange the two operands to this instruction. This |
| 1195 | // instruction is safe to use on any binary instruction and does not |
| 1196 | // modify the semantics of the instruction. If the instruction is |
| 1197 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1198 | // |
| 1199 | bool BinaryOperator::swapOperands() { |
| 1200 | if (isCommutative()) |
| 1201 | ; // If the instruction is commutative, it is safe to swap the operands |
| 1202 | else if (SetCondInst *SCI = dyn_cast<SetCondInst>(this)) |
| 1203 | /// FIXME: SetCC instructions shouldn't all have different opcodes. |
| 1204 | setOpcode(SCI->getSwappedCondition()); |
| 1205 | else |
| 1206 | return true; // Can't commute operands |
| 1207 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1208 | std::swap(Ops[0], Ops[1]); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | //===----------------------------------------------------------------------===// |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 1214 | // ShiftInst Class |
| 1215 | //===----------------------------------------------------------------------===// |
| 1216 | |
| 1217 | /// isLogicalShift - Return true if this is a logical shift left or a logical |
| 1218 | /// shift right. |
| 1219 | bool ShiftInst::isLogicalShift() const { |
| 1220 | return getOpcode() == Instruction::Shl || getType()->isUnsigned(); |
| 1221 | } |
| 1222 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1223 | //===----------------------------------------------------------------------===// |
| 1224 | // CastInst Class |
| 1225 | //===----------------------------------------------------------------------===// |
| 1226 | |
| 1227 | /// isTruncIntCast - Return true if this is a truncating integer cast |
| 1228 | /// instruction, e.g. a cast from long to uint. |
| 1229 | bool CastInst::isTruncIntCast() const { |
| 1230 | // The dest type has to be integral, the input has to be integer. |
| 1231 | if (!getType()->isIntegral() || !getOperand(0)->getType()->isInteger()) |
| 1232 | return false; |
| 1233 | |
| 1234 | // Has to be large to smaller. |
| 1235 | return getOperand(0)->getType()->getPrimitiveSizeInBits() > |
| 1236 | getType()->getPrimitiveSizeInBits(); |
| 1237 | } |
| 1238 | |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 1239 | |
| 1240 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1241 | // SetCondInst Class |
| 1242 | //===----------------------------------------------------------------------===// |
| 1243 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1244 | SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1245 | const std::string &Name, Instruction *InsertBefore) |
| 1246 | : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) { |
| 1247 | |
| 1248 | // Make sure it's a valid type... getInverseCondition will assert out if not. |
| 1249 | assert(getInverseCondition(Opcode)); |
| 1250 | } |
| 1251 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1252 | SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1253 | const std::string &Name, BasicBlock *InsertAtEnd) |
| 1254 | : BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) { |
| 1255 | |
| 1256 | // Make sure it's a valid type... getInverseCondition will assert out if not. |
| 1257 | assert(getInverseCondition(Opcode)); |
| 1258 | } |
| 1259 | |
| 1260 | // getInverseCondition - Return the inverse of the current condition opcode. |
| 1261 | // For example seteq -> setne, setgt -> setle, setlt -> setge, etc... |
| 1262 | // |
| 1263 | Instruction::BinaryOps SetCondInst::getInverseCondition(BinaryOps Opcode) { |
| 1264 | switch (Opcode) { |
| 1265 | default: |
| 1266 | assert(0 && "Unknown setcc opcode!"); |
| 1267 | case SetEQ: return SetNE; |
| 1268 | case SetNE: return SetEQ; |
| 1269 | case SetGT: return SetLE; |
| 1270 | case SetLT: return SetGE; |
| 1271 | case SetGE: return SetLT; |
| 1272 | case SetLE: return SetGT; |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | // getSwappedCondition - Return the condition opcode that would be the result |
| 1277 | // of exchanging the two operands of the setcc instruction without changing |
| 1278 | // the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc. |
| 1279 | // |
| 1280 | Instruction::BinaryOps SetCondInst::getSwappedCondition(BinaryOps Opcode) { |
| 1281 | switch (Opcode) { |
| 1282 | default: assert(0 && "Unknown setcc instruction!"); |
| 1283 | case SetEQ: case SetNE: return Opcode; |
| 1284 | case SetGT: return SetLT; |
| 1285 | case SetLT: return SetGT; |
| 1286 | case SetGE: return SetLE; |
| 1287 | case SetLE: return SetGE; |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | //===----------------------------------------------------------------------===// |
| 1292 | // SwitchInst Implementation |
| 1293 | //===----------------------------------------------------------------------===// |
| 1294 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1295 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1296 | assert(Value && Default); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1297 | ReservedSpace = 2+NumCases*2; |
| 1298 | NumOperands = 2; |
| 1299 | OperandList = new Use[ReservedSpace]; |
| 1300 | |
| 1301 | OperandList[0].init(Value, this); |
| 1302 | OperandList[1].init(Default, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1305 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1306 | : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()], |
| 1307 | SI.getNumOperands()) { |
| 1308 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 1309 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
| 1310 | OL[i].init(InOL[i], this); |
| 1311 | OL[i+1].init(InOL[i+1], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1312 | } |
| 1313 | } |
| 1314 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1315 | SwitchInst::~SwitchInst() { |
| 1316 | delete [] OperandList; |
| 1317 | } |
| 1318 | |
| 1319 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1320 | /// addCase - Add an entry to the switch instruction... |
| 1321 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 1322 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1323 | unsigned OpNo = NumOperands; |
| 1324 | if (OpNo+2 > ReservedSpace) |
| 1325 | resizeOperands(0); // Get more space! |
| 1326 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 1327 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1328 | NumOperands = OpNo+2; |
| 1329 | OperandList[OpNo].init(OnVal, this); |
| 1330 | OperandList[OpNo+1].init(Dest, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | /// removeCase - This method removes the specified successor from the switch |
| 1334 | /// instruction. Note that this cannot be used to remove the default |
| 1335 | /// destination (successor #0). |
| 1336 | /// |
| 1337 | void SwitchInst::removeCase(unsigned idx) { |
| 1338 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1339 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 1340 | |
| 1341 | unsigned NumOps = getNumOperands(); |
| 1342 | Use *OL = OperandList; |
| 1343 | |
| 1344 | // Move everything after this operand down. |
| 1345 | // |
| 1346 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 1347 | // client might not expect this to happen. The code as it is thrashes the |
| 1348 | // use/def lists, which is kinda lame. |
| 1349 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 1350 | OL[i-2] = OL[i]; |
| 1351 | OL[i-2+1] = OL[i+1]; |
| 1352 | } |
| 1353 | |
| 1354 | // Nuke the last value. |
| 1355 | OL[NumOps-2].set(0); |
| 1356 | OL[NumOps-2+1].set(0); |
| 1357 | NumOperands = NumOps-2; |
| 1358 | } |
| 1359 | |
| 1360 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 1361 | /// list according to the following behavior: |
| 1362 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 1363 | /// of operation. This grows the number of ops by 1.5 times. |
| 1364 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 1365 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 1366 | /// |
| 1367 | void SwitchInst::resizeOperands(unsigned NumOps) { |
| 1368 | if (NumOps == 0) { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 1369 | NumOps = getNumOperands()/2*6; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1370 | } else if (NumOps*2 > NumOperands) { |
| 1371 | // No resize needed. |
| 1372 | if (ReservedSpace >= NumOps) return; |
| 1373 | } else if (NumOps == NumOperands) { |
| 1374 | if (ReservedSpace == NumOps) return; |
| 1375 | } else { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 1376 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | ReservedSpace = NumOps; |
| 1380 | Use *NewOps = new Use[NumOps]; |
| 1381 | Use *OldOps = OperandList; |
| 1382 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1383 | NewOps[i].init(OldOps[i], this); |
| 1384 | OldOps[i].set(0); |
| 1385 | } |
| 1386 | delete [] OldOps; |
| 1387 | OperandList = NewOps; |
| 1388 | } |
| 1389 | |
| 1390 | |
| 1391 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 1392 | return getSuccessor(idx); |
| 1393 | } |
| 1394 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 1395 | return getNumSuccessors(); |
| 1396 | } |
| 1397 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 1398 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1399 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1400 | |
| 1401 | |
| 1402 | // Define these methods here so vtables don't get emitted into every translation |
| 1403 | // unit that uses these classes. |
| 1404 | |
| 1405 | GetElementPtrInst *GetElementPtrInst::clone() const { |
| 1406 | return new GetElementPtrInst(*this); |
| 1407 | } |
| 1408 | |
| 1409 | BinaryOperator *BinaryOperator::clone() const { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1410 | return create(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | MallocInst *MallocInst::clone() const { return new MallocInst(*this); } |
| 1414 | AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1415 | FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1416 | LoadInst *LoadInst::clone() const { return new LoadInst(*this); } |
| 1417 | StoreInst *StoreInst::clone() const { return new StoreInst(*this); } |
| 1418 | CastInst *CastInst::clone() const { return new CastInst(*this); } |
| 1419 | CallInst *CallInst::clone() const { return new CallInst(*this); } |
| 1420 | ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); } |
| 1421 | SelectInst *SelectInst::clone() const { return new SelectInst(*this); } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1422 | VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1423 | ExtractElementInst *ExtractElementInst::clone() const { |
| 1424 | return new ExtractElementInst(*this); |
| 1425 | } |
| 1426 | InsertElementInst *InsertElementInst::clone() const { |
| 1427 | return new InsertElementInst(*this); |
| 1428 | } |
| 1429 | ShuffleVectorInst *ShuffleVectorInst::clone() const { |
| 1430 | return new ShuffleVectorInst(*this); |
| 1431 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 1432 | PHINode *PHINode::clone() const { return new PHINode(*this); } |
| 1433 | ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); } |
| 1434 | BranchInst *BranchInst::clone() const { return new BranchInst(*this); } |
| 1435 | SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } |
| 1436 | InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } |
| 1437 | UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1438 | UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} |