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()) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 485 | assert(getCondition()->getType() == Type::Int1Ty && |
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 | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 516 | Amt = ConstantInt::get(Type::Int32Ty, 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"); |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 520 | assert(Amt->getType() == Type::Int32Ty && |
Reid Spencer | 7e16e23 | 2007-01-26 06:30:34 +0000 | [diff] [blame] | 521 | "Malloc/Allocation array size is not a 32-bit integer!"); |
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 | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame^] | 687 | void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) { |
| 688 | NumOperands = 1+NumIdx; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 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 | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame^] | 692 | for (unsigned i = 0; i != NumIdx; ++i) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame^] | 716 | init(Ptr, &Idx[0], Idx.size()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame^] | 724 | init(Ptr, &Idx[0], Idx.size()); |
| 725 | } |
| 726 | |
| 727 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx, |
| 728 | unsigned NumIdx, |
| 729 | const std::string &Name, Instruction *InBe) |
| 730 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 731 | Idx, true))), |
| 732 | GetElementPtr, 0, 0, Name, InBe) { |
| 733 | init(Ptr, Idx, NumIdx); |
| 734 | } |
| 735 | |
| 736 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx, |
| 737 | unsigned NumIdx, |
| 738 | const std::string &Name, BasicBlock *IAE) |
| 739 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 740 | Idx, true))), |
| 741 | GetElementPtr, 0, 0, Name, IAE) { |
| 742 | init(Ptr, Idx, NumIdx); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 745 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 746 | const std::string &Name, Instruction *InBe) |
| 747 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 748 | GetElementPtr, 0, 0, Name, InBe) { |
| 749 | init(Ptr, Idx); |
| 750 | } |
| 751 | |
| 752 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
| 753 | const std::string &Name, BasicBlock *IAE) |
| 754 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), |
| 755 | GetElementPtr, 0, 0, Name, IAE) { |
| 756 | init(Ptr, Idx); |
| 757 | } |
| 758 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 759 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
| 760 | const std::string &Name, Instruction *InBe) |
| 761 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 762 | Idx0, Idx1, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 763 | GetElementPtr, 0, 0, Name, InBe) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 764 | init(Ptr, Idx0, Idx1); |
| 765 | } |
| 766 | |
| 767 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 768 | const std::string &Name, BasicBlock *IAE) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 769 | : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), |
| 770 | Idx0, Idx1, true))), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 771 | GetElementPtr, 0, 0, Name, IAE) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 772 | init(Ptr, Idx0, Idx1); |
| 773 | } |
| 774 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 775 | GetElementPtrInst::~GetElementPtrInst() { |
| 776 | delete[] OperandList; |
| 777 | } |
| 778 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 779 | // getIndexedType - Returns the type of the element that would be loaded with |
| 780 | // a load instruction with the specified parameters. |
| 781 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 782 | // 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] | 783 | // pointer type. |
| 784 | // |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 785 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 786 | Value* const *Idxs, |
| 787 | unsigned NumIdx, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 788 | bool AllowCompositeLeaf) { |
| 789 | if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type! |
| 790 | |
| 791 | // Handle the special case of the empty set index set... |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 792 | if (NumIdx == 0) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 793 | if (AllowCompositeLeaf || |
| 794 | cast<PointerType>(Ptr)->getElementType()->isFirstClassType()) |
| 795 | return cast<PointerType>(Ptr)->getElementType(); |
| 796 | else |
| 797 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 798 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 799 | unsigned CurIdx = 0; |
| 800 | while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 801 | if (NumIdx == CurIdx) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 802 | if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; |
| 803 | return 0; // Can't load a whole structure or array!?!? |
| 804 | } |
| 805 | |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 806 | Value *Index = Idxs[CurIdx++]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 807 | if (isa<PointerType>(CT) && CurIdx != 1) |
| 808 | return 0; // Can only index into pointer types at the first index! |
| 809 | if (!CT->indexValid(Index)) return 0; |
| 810 | Ptr = CT->getTypeAtIndex(Index); |
| 811 | |
| 812 | // If the new type forwards to another type, then it is in the middle |
| 813 | // of being refined to another type (and hence, may have dropped all |
| 814 | // references to what it was using before). So, use the new forwarded |
| 815 | // type. |
| 816 | if (const Type * Ty = Ptr->getForwardedType()) { |
| 817 | Ptr = Ty; |
| 818 | } |
| 819 | } |
Chris Lattner | 302116a | 2007-01-31 04:40:28 +0000 | [diff] [blame] | 820 | return CurIdx == NumIdx ? Ptr : 0; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 823 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 824 | Value *Idx0, Value *Idx1, |
| 825 | bool AllowCompositeLeaf) { |
| 826 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 827 | if (!PTy) return 0; // Type isn't a pointer type! |
| 828 | |
| 829 | // Check the pointer index. |
| 830 | if (!PTy->indexValid(Idx0)) return 0; |
| 831 | |
| 832 | const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType()); |
| 833 | if (!CT || !CT->indexValid(Idx1)) return 0; |
| 834 | |
| 835 | const Type *ElTy = CT->getTypeAtIndex(Idx1); |
| 836 | if (AllowCompositeLeaf || ElTy->isFirstClassType()) |
| 837 | return ElTy; |
| 838 | return 0; |
| 839 | } |
| 840 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 841 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 842 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 843 | if (!PTy) return 0; // Type isn't a pointer type! |
| 844 | |
| 845 | // Check the pointer index. |
| 846 | if (!PTy->indexValid(Idx)) return 0; |
| 847 | |
Chris Lattner | c223333 | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 848 | return PTy->getElementType(); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 851 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 852 | // ExtractElementInst Implementation |
| 853 | //===----------------------------------------------------------------------===// |
| 854 | |
| 855 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 856 | const std::string &Name, |
| 857 | Instruction *InsertBef) |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 858 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 859 | ExtractElement, Ops, 2, Name, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 860 | assert(isValidOperands(Val, Index) && |
| 861 | "Invalid extractelement instruction operands!"); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 862 | Ops[0].init(Val, this); |
| 863 | Ops[1].init(Index, this); |
| 864 | } |
| 865 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 866 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 867 | const std::string &Name, |
| 868 | Instruction *InsertBef) |
| 869 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 870 | ExtractElement, Ops, 2, Name, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 871 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 872 | assert(isValidOperands(Val, Index) && |
| 873 | "Invalid extractelement instruction operands!"); |
| 874 | Ops[0].init(Val, this); |
| 875 | Ops[1].init(Index, this); |
| 876 | } |
| 877 | |
| 878 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 879 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 880 | const std::string &Name, |
| 881 | BasicBlock *InsertAE) |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 882 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 883 | ExtractElement, Ops, 2, Name, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 884 | assert(isValidOperands(Val, Index) && |
| 885 | "Invalid extractelement instruction operands!"); |
| 886 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 887 | Ops[0].init(Val, this); |
| 888 | Ops[1].init(Index, this); |
| 889 | } |
| 890 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 891 | ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, |
| 892 | const std::string &Name, |
| 893 | BasicBlock *InsertAE) |
| 894 | : Instruction(cast<PackedType>(Val->getType())->getElementType(), |
| 895 | ExtractElement, Ops, 2, Name, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 896 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 897 | assert(isValidOperands(Val, Index) && |
| 898 | "Invalid extractelement instruction operands!"); |
| 899 | |
| 900 | Ops[0].init(Val, this); |
| 901 | Ops[1].init(Index, this); |
| 902 | } |
| 903 | |
| 904 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 905 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 906 | if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 907 | return false; |
| 908 | return true; |
| 909 | } |
| 910 | |
| 911 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 912 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 913 | // InsertElementInst Implementation |
| 914 | //===----------------------------------------------------------------------===// |
| 915 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 916 | InsertElementInst::InsertElementInst(const InsertElementInst &IE) |
| 917 | : Instruction(IE.getType(), InsertElement, Ops, 3) { |
| 918 | Ops[0].init(IE.Ops[0], this); |
| 919 | Ops[1].init(IE.Ops[1], this); |
| 920 | Ops[2].init(IE.Ops[2], this); |
| 921 | } |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 922 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 923 | const std::string &Name, |
| 924 | Instruction *InsertBef) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 925 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { |
| 926 | assert(isValidOperands(Vec, Elt, Index) && |
| 927 | "Invalid insertelement instruction operands!"); |
| 928 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 929 | Ops[1].init(Elt, this); |
| 930 | Ops[2].init(Index, this); |
| 931 | } |
| 932 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 933 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 934 | const std::string &Name, |
| 935 | Instruction *InsertBef) |
| 936 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 937 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 938 | assert(isValidOperands(Vec, Elt, Index) && |
| 939 | "Invalid insertelement instruction operands!"); |
| 940 | Ops[0].init(Vec, this); |
| 941 | Ops[1].init(Elt, this); |
| 942 | Ops[2].init(Index, this); |
| 943 | } |
| 944 | |
| 945 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 946 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 947 | const std::string &Name, |
| 948 | BasicBlock *InsertAE) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 949 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { |
| 950 | assert(isValidOperands(Vec, Elt, Index) && |
| 951 | "Invalid insertelement instruction operands!"); |
| 952 | |
| 953 | Ops[0].init(Vec, this); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 954 | Ops[1].init(Elt, this); |
| 955 | Ops[2].init(Index, this); |
| 956 | } |
| 957 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 958 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, |
| 959 | const std::string &Name, |
| 960 | BasicBlock *InsertAE) |
| 961 | : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 962 | Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 963 | assert(isValidOperands(Vec, Elt, Index) && |
| 964 | "Invalid insertelement instruction operands!"); |
| 965 | |
| 966 | Ops[0].init(Vec, this); |
| 967 | Ops[1].init(Elt, this); |
| 968 | Ops[2].init(Index, this); |
| 969 | } |
| 970 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 971 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 972 | const Value *Index) { |
| 973 | if (!isa<PackedType>(Vec->getType())) |
| 974 | return false; // First operand of insertelement must be packed type. |
| 975 | |
| 976 | if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType()) |
| 977 | return false;// Second operand of insertelement must be packed element type. |
| 978 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 979 | if (Index->getType() != Type::Int32Ty) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 980 | return false; // Third operand of insertelement must be uint. |
| 981 | return true; |
| 982 | } |
| 983 | |
| 984 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 985 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 986 | // ShuffleVectorInst Implementation |
| 987 | //===----------------------------------------------------------------------===// |
| 988 | |
Chris Lattner | 0875d94 | 2006-04-14 22:20:32 +0000 | [diff] [blame] | 989 | ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) |
| 990 | : Instruction(SV.getType(), ShuffleVector, Ops, 3) { |
| 991 | Ops[0].init(SV.Ops[0], this); |
| 992 | Ops[1].init(SV.Ops[1], this); |
| 993 | Ops[2].init(SV.Ops[2], this); |
| 994 | } |
| 995 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 996 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 997 | const std::string &Name, |
| 998 | Instruction *InsertBefore) |
| 999 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) { |
| 1000 | assert(isValidOperands(V1, V2, Mask) && |
| 1001 | "Invalid shuffle vector instruction operands!"); |
| 1002 | Ops[0].init(V1, this); |
| 1003 | Ops[1].init(V2, this); |
| 1004 | Ops[2].init(Mask, this); |
| 1005 | } |
| 1006 | |
| 1007 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
| 1008 | const std::string &Name, |
| 1009 | BasicBlock *InsertAtEnd) |
| 1010 | : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) { |
| 1011 | assert(isValidOperands(V1, V2, Mask) && |
| 1012 | "Invalid shuffle vector instruction operands!"); |
| 1013 | |
| 1014 | Ops[0].init(V1, this); |
| 1015 | Ops[1].init(V2, this); |
| 1016 | Ops[2].init(Mask, this); |
| 1017 | } |
| 1018 | |
| 1019 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
| 1020 | const Value *Mask) { |
| 1021 | if (!isa<PackedType>(V1->getType())) return false; |
| 1022 | if (V1->getType() != V2->getType()) return false; |
| 1023 | if (!isa<PackedType>(Mask->getType()) || |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1024 | cast<PackedType>(Mask->getType())->getElementType() != Type::Int32Ty || |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1025 | cast<PackedType>(Mask->getType())->getNumElements() != |
| 1026 | cast<PackedType>(V1->getType())->getNumElements()) |
| 1027 | return false; |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
| 1031 | |
| 1032 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1033 | // BinaryOperator Class |
| 1034 | //===----------------------------------------------------------------------===// |
| 1035 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1036 | void BinaryOperator::init(BinaryOps iType) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1037 | { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1038 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
| 1039 | assert(LHS->getType() == RHS->getType() && |
| 1040 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1041 | #ifndef NDEBUG |
| 1042 | switch (iType) { |
| 1043 | case Add: case Sub: |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1044 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1045 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1046 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1047 | assert((getType()->isInteger() || getType()->isFloatingPoint() || |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1048 | isa<PackedType>(getType())) && |
Brian Gaeke | 0220904 | 2004-08-20 06:00:58 +0000 | [diff] [blame] | 1049 | "Tried to create an arithmetic operation on a non-arithmetic type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1050 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1051 | case UDiv: |
| 1052 | case SDiv: |
| 1053 | assert(getType() == LHS->getType() && |
| 1054 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1055 | assert((getType()->isInteger() || (isa<PackedType>(getType()) && |
| 1056 | cast<PackedType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1057 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1058 | break; |
| 1059 | case FDiv: |
| 1060 | assert(getType() == LHS->getType() && |
| 1061 | "Arithmetic operation should return same type as operands!"); |
| 1062 | assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) && |
| 1063 | cast<PackedType>(getType())->getElementType()->isFloatingPoint())) |
| 1064 | && "Incorrect operand type (not floating point) for FDIV"); |
| 1065 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1066 | case URem: |
| 1067 | case SRem: |
| 1068 | assert(getType() == LHS->getType() && |
| 1069 | "Arithmetic operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1070 | assert((getType()->isInteger() || (isa<PackedType>(getType()) && |
| 1071 | cast<PackedType>(getType())->getElementType()->isInteger())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1072 | "Incorrect operand type (not integer) for S/UREM"); |
| 1073 | break; |
| 1074 | case FRem: |
| 1075 | assert(getType() == LHS->getType() && |
| 1076 | "Arithmetic operation should return same type as operands!"); |
| 1077 | assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) && |
| 1078 | cast<PackedType>(getType())->getElementType()->isFloatingPoint())) |
| 1079 | && "Incorrect operand type (not floating point) for FREM"); |
| 1080 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1081 | case And: case Or: |
| 1082 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1083 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1084 | "Logical operation should return same type as operands!"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1085 | assert((getType()->isInteger() || |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1086 | (isa<PackedType>(getType()) && |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1087 | cast<PackedType>(getType())->getElementType()->isInteger())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1088 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1089 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1090 | default: |
| 1091 | break; |
| 1092 | } |
| 1093 | #endif |
| 1094 | } |
| 1095 | |
| 1096 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1097 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1098 | Instruction *InsertBefore) { |
| 1099 | assert(S1->getType() == S2->getType() && |
| 1100 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1101 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, |
Misha Brukman | 96eb878 | 2005-03-16 05:42:00 +0000 | [diff] [blame] | 1105 | const std::string &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1106 | BasicBlock *InsertAtEnd) { |
| 1107 | BinaryOperator *Res = create(Op, S1, S2, Name); |
| 1108 | InsertAtEnd->getInstList().push_back(Res); |
| 1109 | return Res; |
| 1110 | } |
| 1111 | |
| 1112 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1113 | Instruction *InsertBefore) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1114 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1115 | return new BinaryOperator(Instruction::Sub, |
| 1116 | zero, Op, |
| 1117 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, |
| 1121 | BasicBlock *InsertAtEnd) { |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1122 | Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); |
| 1123 | return new BinaryOperator(Instruction::Sub, |
| 1124 | zero, Op, |
| 1125 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1129 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1130 | Constant *C; |
| 1131 | if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1132 | C = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1133 | C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C)); |
| 1134 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1135 | C = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1139 | Op->getType(), Name, InsertBefore); |
| 1140 | } |
| 1141 | |
| 1142 | BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, |
| 1143 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1144 | Constant *AllOnes; |
| 1145 | if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) { |
| 1146 | // Create a vector of all ones values. |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1147 | Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1148 | AllOnes = |
| 1149 | ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt)); |
| 1150 | } else { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1151 | AllOnes = ConstantInt::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1155 | Op->getType(), Name, InsertAtEnd); |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | // isConstantAllOnes - Helper function for several functions below |
| 1160 | static inline bool isConstantAllOnes(const Value *V) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1161 | return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | bool BinaryOperator::isNeg(const Value *V) { |
| 1165 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1166 | if (Bop->getOpcode() == Instruction::Sub) |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1167 | return Bop->getOperand(0) == |
| 1168 | ConstantExpr::getZeroValueForNegationExpr(Bop->getType()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1169 | return false; |
| 1170 | } |
| 1171 | |
| 1172 | bool BinaryOperator::isNot(const Value *V) { |
| 1173 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1174 | return (Bop->getOpcode() == Instruction::Xor && |
| 1175 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1176 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1180 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
| 1181 | assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!"); |
| 1182 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1185 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1186 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1189 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1190 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1191 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1192 | Value *Op0 = BO->getOperand(0); |
| 1193 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1194 | if (isConstantAllOnes(Op0)) return Op1; |
| 1195 | |
| 1196 | assert(isConstantAllOnes(Op1)); |
| 1197 | return Op0; |
| 1198 | } |
| 1199 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1200 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1201 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | |
| 1205 | // swapOperands - Exchange the two operands to this instruction. This |
| 1206 | // instruction is safe to use on any binary instruction and does not |
| 1207 | // modify the semantics of the instruction. If the instruction is |
| 1208 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1209 | // |
| 1210 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1211 | if (!isCommutative()) |
| 1212 | return true; // Can't commute operands |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1213 | std::swap(Ops[0], Ops[1]); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1214 | return false; |
| 1215 | } |
| 1216 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1217 | //===----------------------------------------------------------------------===// |
| 1218 | // CastInst Class |
| 1219 | //===----------------------------------------------------------------------===// |
| 1220 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1221 | // Just determine if this cast only deals with integral->integral conversion. |
| 1222 | bool CastInst::isIntegerCast() const { |
| 1223 | switch (getOpcode()) { |
| 1224 | default: return false; |
| 1225 | case Instruction::ZExt: |
| 1226 | case Instruction::SExt: |
| 1227 | case Instruction::Trunc: |
| 1228 | return true; |
| 1229 | case Instruction::BitCast: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1230 | return getOperand(0)->getType()->isInteger() && getType()->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1231 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1234 | bool CastInst::isLosslessCast() const { |
| 1235 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 1236 | if (getOpcode() != Instruction::BitCast) |
| 1237 | return false; |
| 1238 | |
| 1239 | // Identity cast is always lossless |
| 1240 | const Type* SrcTy = getOperand(0)->getType(); |
| 1241 | const Type* DstTy = getType(); |
| 1242 | if (SrcTy == DstTy) |
| 1243 | return true; |
| 1244 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1245 | // Pointer to pointer is always lossless. |
| 1246 | if (isa<PointerType>(SrcTy)) |
| 1247 | return isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1248 | return false; // Other types have no identity values |
| 1249 | } |
| 1250 | |
| 1251 | /// This function determines if the CastInst does not require any bits to be |
| 1252 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 1253 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 1254 | /// example, the following are all no-op casts: |
| 1255 | /// # bitcast uint %X, int |
| 1256 | /// # bitcast uint* %x, sbyte* |
| 1257 | /// # bitcast packed< 2 x int > %x, packed< 4 x short> |
| 1258 | /// # ptrtoint uint* %x, uint ; on 32-bit plaforms only |
| 1259 | /// @brief Determine if a cast is a no-op. |
| 1260 | bool CastInst::isNoopCast(const Type *IntPtrTy) const { |
| 1261 | switch (getOpcode()) { |
| 1262 | default: |
| 1263 | assert(!"Invalid CastOp"); |
| 1264 | case Instruction::Trunc: |
| 1265 | case Instruction::ZExt: |
| 1266 | case Instruction::SExt: |
| 1267 | case Instruction::FPTrunc: |
| 1268 | case Instruction::FPExt: |
| 1269 | case Instruction::UIToFP: |
| 1270 | case Instruction::SIToFP: |
| 1271 | case Instruction::FPToUI: |
| 1272 | case Instruction::FPToSI: |
| 1273 | return false; // These always modify bits |
| 1274 | case Instruction::BitCast: |
| 1275 | return true; // BitCast never modifies bits. |
| 1276 | case Instruction::PtrToInt: |
| 1277 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1278 | getType()->getPrimitiveSizeInBits(); |
| 1279 | case Instruction::IntToPtr: |
| 1280 | return IntPtrTy->getPrimitiveSizeInBits() == |
| 1281 | getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | /// This function determines if a pair of casts can be eliminated and what |
| 1286 | /// opcode should be used in the elimination. This assumes that there are two |
| 1287 | /// instructions like this: |
| 1288 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 1289 | /// * %S = secondOpcode MidTy %F to DstTy |
| 1290 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 1291 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 1292 | /// If no such cast is permited, the function returns 0. |
| 1293 | unsigned CastInst::isEliminableCastPair( |
| 1294 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
| 1295 | const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy) |
| 1296 | { |
| 1297 | // Define the 144 possibilities for these two cast instructions. The values |
| 1298 | // in this matrix determine what to do in a given situation and select the |
| 1299 | // case in the switch below. The rows correspond to firstOp, the columns |
| 1300 | // correspond to secondOp. In looking at the table below, keep in mind |
| 1301 | // the following cast properties: |
| 1302 | // |
| 1303 | // Size Compare Source Destination |
| 1304 | // Operator Src ? Size Type Sign Type Sign |
| 1305 | // -------- ------------ ------------------- --------------------- |
| 1306 | // TRUNC > Integer Any Integral Any |
| 1307 | // ZEXT < Integral Unsigned Integer Any |
| 1308 | // SEXT < Integral Signed Integer Any |
| 1309 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 1310 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 1311 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 1312 | // SITOFP n/a Integral Signed FloatPt n/a |
| 1313 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 1314 | // FPEXT < FloatPt n/a FloatPt n/a |
| 1315 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 1316 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
| 1317 | // BITCONVERT = FirstClass n/a FirstClass n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1318 | // |
| 1319 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
| 1320 | // For example, we could merge "fptoui double to uint" + "zext uint to ulong", |
| 1321 | // into "fptoui double to ulong", but this loses information about the range |
| 1322 | // of the produced value (we no longer know the top-part is all zeros). |
| 1323 | // Further this conversion is often much more expensive for typical hardware, |
| 1324 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 1325 | // same reason. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1326 | const unsigned numCastOps = |
| 1327 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 1328 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 1329 | // T F F U S F F P I B -+ |
| 1330 | // R Z S P P I I T P 2 N T | |
| 1331 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 1332 | // N X X U S F F N X N 2 V | |
| 1333 | // C T T I I P P C T T P T -+ |
| 1334 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 1335 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 1336 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1337 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 1338 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1339 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 1340 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 1341 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 1342 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 1343 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 1344 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 1345 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 1346 | }; |
| 1347 | |
| 1348 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 1349 | [secondOp-Instruction::CastOpsBegin]; |
| 1350 | switch (ElimCase) { |
| 1351 | case 0: |
| 1352 | // categorically disallowed |
| 1353 | return 0; |
| 1354 | case 1: |
| 1355 | // allowed, use first cast's opcode |
| 1356 | return firstOp; |
| 1357 | case 2: |
| 1358 | // allowed, use second cast's opcode |
| 1359 | return secondOp; |
| 1360 | case 3: |
| 1361 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1362 | // is integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1363 | if (DstTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1364 | return firstOp; |
| 1365 | return 0; |
| 1366 | case 4: |
| 1367 | // no-op cast in second op implies firstOp as long as the DestTy |
| 1368 | // is floating point |
| 1369 | if (DstTy->isFloatingPoint()) |
| 1370 | return firstOp; |
| 1371 | return 0; |
| 1372 | case 5: |
| 1373 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1374 | // is an integer |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1375 | if (SrcTy->isInteger()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1376 | return secondOp; |
| 1377 | return 0; |
| 1378 | case 6: |
| 1379 | // no-op cast in first op implies secondOp as long as the SrcTy |
| 1380 | // is a floating point |
| 1381 | if (SrcTy->isFloatingPoint()) |
| 1382 | return secondOp; |
| 1383 | return 0; |
| 1384 | case 7: { |
| 1385 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
| 1386 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1387 | unsigned MidSize = MidTy->getPrimitiveSizeInBits(); |
| 1388 | if (MidSize >= PtrSize) |
| 1389 | return Instruction::BitCast; |
| 1390 | return 0; |
| 1391 | } |
| 1392 | case 8: { |
| 1393 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 1394 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 1395 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
| 1396 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1397 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1398 | if (SrcSize == DstSize) |
| 1399 | return Instruction::BitCast; |
| 1400 | else if (SrcSize < DstSize) |
| 1401 | return firstOp; |
| 1402 | return secondOp; |
| 1403 | } |
| 1404 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 1405 | return Instruction::ZExt; |
| 1406 | case 10: |
| 1407 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 1408 | // the same as the original, in which case its just a bitcast |
| 1409 | if (SrcTy == DstTy) |
| 1410 | return Instruction::BitCast; |
| 1411 | return 0; // If the types are not the same we can't eliminate it. |
| 1412 | case 11: |
| 1413 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 1414 | // is a pointer to pointer cast. |
| 1415 | if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy)) |
| 1416 | return secondOp; |
| 1417 | return 0; |
| 1418 | case 12: |
| 1419 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
| 1420 | if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy)) |
| 1421 | return firstOp; |
| 1422 | return 0; |
| 1423 | case 13: { |
| 1424 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
| 1425 | unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); |
| 1426 | unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); |
| 1427 | unsigned DstSize = DstTy->getPrimitiveSizeInBits(); |
| 1428 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 1429 | return Instruction::BitCast; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | case 99: |
| 1433 | // cast combination can't happen (error in input). This is for all cases |
| 1434 | // where the MidTy is not the same for the two cast instructions. |
| 1435 | assert(!"Invalid Cast Combination"); |
| 1436 | return 0; |
| 1437 | default: |
| 1438 | assert(!"Error in CastResults table!!!"); |
| 1439 | return 0; |
| 1440 | } |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1445 | const std::string &Name, Instruction *InsertBefore) { |
| 1446 | // Construct and return the appropriate CastInst subclass |
| 1447 | switch (op) { |
| 1448 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 1449 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 1450 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 1451 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 1452 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 1453 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 1454 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 1455 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 1456 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 1457 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 1458 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 1459 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 1460 | default: |
| 1461 | assert(!"Invalid opcode provided"); |
| 1462 | } |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, |
| 1467 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 1468 | // Construct and return the appropriate CastInst subclass |
| 1469 | switch (op) { |
| 1470 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 1471 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 1472 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 1473 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 1474 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 1475 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1476 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 1477 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 1478 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 1479 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 1480 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 1481 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 1482 | default: |
| 1483 | assert(!"Invalid opcode provided"); |
| 1484 | } |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 1488 | CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, |
| 1489 | const std::string &Name, |
| 1490 | Instruction *InsertBefore) { |
| 1491 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1492 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1493 | return create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
| 1494 | } |
| 1495 | |
| 1496 | CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, |
| 1497 | const std::string &Name, |
| 1498 | BasicBlock *InsertAtEnd) { |
| 1499 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1500 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1501 | return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
| 1502 | } |
| 1503 | |
| 1504 | CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, |
| 1505 | const std::string &Name, |
| 1506 | Instruction *InsertBefore) { |
| 1507 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1508 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1509 | return create(Instruction::SExt, S, Ty, Name, InsertBefore); |
| 1510 | } |
| 1511 | |
| 1512 | CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, |
| 1513 | const std::string &Name, |
| 1514 | BasicBlock *InsertAtEnd) { |
| 1515 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1516 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1517 | return create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
| 1518 | } |
| 1519 | |
| 1520 | CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, |
| 1521 | const std::string &Name, |
| 1522 | Instruction *InsertBefore) { |
| 1523 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1524 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1525 | return create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
| 1526 | } |
| 1527 | |
| 1528 | CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, |
| 1529 | const std::string &Name, |
| 1530 | BasicBlock *InsertAtEnd) { |
| 1531 | if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) |
| 1532 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1533 | return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
| 1534 | } |
| 1535 | |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1536 | CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, |
| 1537 | const std::string &Name, |
| 1538 | BasicBlock *InsertAtEnd) { |
| 1539 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1540 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1541 | "Invalid cast"); |
| 1542 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1543 | if (Ty->isInteger()) |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1544 | return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 1545 | return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 1546 | } |
| 1547 | |
| 1548 | /// @brief Create a BitCast or a PtrToInt cast instruction |
| 1549 | CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, |
| 1550 | const std::string &Name, |
| 1551 | Instruction *InsertBefore) { |
| 1552 | assert(isa<PointerType>(S->getType()) && "Invalid cast"); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1553 | assert((Ty->isInteger() || isa<PointerType>(Ty)) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1554 | "Invalid cast"); |
| 1555 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1556 | if (Ty->isInteger()) |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 1557 | return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 1558 | return create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 1559 | } |
| 1560 | |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1561 | CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, |
| 1562 | bool isSigned, const std::string &Name, |
| 1563 | Instruction *InsertBefore) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1564 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1565 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1566 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1567 | Instruction::CastOps opcode = |
| 1568 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1569 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1570 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1571 | return create(opcode, C, Ty, Name, InsertBefore); |
| 1572 | } |
| 1573 | |
| 1574 | CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, |
| 1575 | bool isSigned, const std::string &Name, |
| 1576 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1577 | assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 1578 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1579 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1580 | Instruction::CastOps opcode = |
| 1581 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1582 | (SrcBits > DstBits ? Instruction::Trunc : |
| 1583 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
| 1584 | return create(opcode, C, Ty, Name, InsertAtEnd); |
| 1585 | } |
| 1586 | |
| 1587 | CastInst *CastInst::createFPCast(Value *C, const Type *Ty, |
| 1588 | const std::string &Name, |
| 1589 | Instruction *InsertBefore) { |
| 1590 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1591 | "Invalid cast"); |
| 1592 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1593 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1594 | Instruction::CastOps opcode = |
| 1595 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1596 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
| 1597 | return create(opcode, C, Ty, Name, InsertBefore); |
| 1598 | } |
| 1599 | |
| 1600 | CastInst *CastInst::createFPCast(Value *C, const Type *Ty, |
| 1601 | const std::string &Name, |
| 1602 | BasicBlock *InsertAtEnd) { |
| 1603 | assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && |
| 1604 | "Invalid cast"); |
| 1605 | unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); |
| 1606 | unsigned DstBits = Ty->getPrimitiveSizeInBits(); |
| 1607 | Instruction::CastOps opcode = |
| 1608 | (SrcBits == DstBits ? Instruction::BitCast : |
| 1609 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
| 1610 | return create(opcode, C, Ty, Name, InsertAtEnd); |
| 1611 | } |
| 1612 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1613 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 1614 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1615 | // logic in the castIsValid function below. This axiom should hold: |
| 1616 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 1617 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1618 | // casting opcode for the arguments passed to it. |
| 1619 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1620 | CastInst::getCastOpcode( |
| 1621 | const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1622 | // Get the bit sizes, we'll need these |
| 1623 | const Type *SrcTy = Src->getType(); |
| 1624 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed |
| 1625 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed |
| 1626 | |
| 1627 | // Run through the possibilities ... |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1628 | if (DestTy->isInteger()) { // Casting to integral |
| 1629 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1630 | if (DestBits < SrcBits) |
| 1631 | return Trunc; // int -> smaller int |
| 1632 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1633 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1634 | return SExt; // signed -> SEXT |
| 1635 | else |
| 1636 | return ZExt; // unsigned -> ZEXT |
| 1637 | } else { |
| 1638 | return BitCast; // Same size, No-op cast |
| 1639 | } |
| 1640 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1641 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1642 | return FPToSI; // FP -> sint |
| 1643 | else |
| 1644 | return FPToUI; // FP -> uint |
| 1645 | } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) { |
| 1646 | assert(DestBits == PTy->getBitWidth() && |
| 1647 | "Casting packed to integer of different width"); |
| 1648 | return BitCast; // Same size, no-op cast |
| 1649 | } else { |
| 1650 | assert(isa<PointerType>(SrcTy) && |
| 1651 | "Casting from a value that is not first-class type"); |
| 1652 | return PtrToInt; // ptr -> int |
| 1653 | } |
| 1654 | } else if (DestTy->isFloatingPoint()) { // Casting to floating pt |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1655 | if (SrcTy->isInteger()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 1656 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1657 | return SIToFP; // sint -> FP |
| 1658 | else |
| 1659 | return UIToFP; // uint -> FP |
| 1660 | } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt |
| 1661 | if (DestBits < SrcBits) { |
| 1662 | return FPTrunc; // FP -> smaller FP |
| 1663 | } else if (DestBits > SrcBits) { |
| 1664 | return FPExt; // FP -> larger FP |
| 1665 | } else { |
| 1666 | return BitCast; // same size, no-op cast |
| 1667 | } |
| 1668 | } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) { |
| 1669 | assert(DestBits == PTy->getBitWidth() && |
| 1670 | "Casting packed to floating point of different width"); |
| 1671 | return BitCast; // same size, no-op cast |
| 1672 | } else { |
| 1673 | assert(0 && "Casting pointer or non-first class to float"); |
| 1674 | } |
| 1675 | } else if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) { |
| 1676 | if (const PackedType *SrcPTy = dyn_cast<PackedType>(SrcTy)) { |
| 1677 | assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && |
| 1678 | "Casting packed to packed of different widths"); |
| 1679 | return BitCast; // packed -> packed |
| 1680 | } else if (DestPTy->getBitWidth() == SrcBits) { |
| 1681 | return BitCast; // float/int -> packed |
| 1682 | } else { |
| 1683 | assert(!"Illegal cast to packed (wrong type or size)"); |
| 1684 | } |
| 1685 | } else if (isa<PointerType>(DestTy)) { |
| 1686 | if (isa<PointerType>(SrcTy)) { |
| 1687 | return BitCast; // ptr -> ptr |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1688 | } else if (SrcTy->isInteger()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1689 | return IntToPtr; // int -> ptr |
| 1690 | } else { |
| 1691 | assert(!"Casting pointer to other than pointer or int"); |
| 1692 | } |
| 1693 | } else { |
| 1694 | assert(!"Casting to type that is not first-class"); |
| 1695 | } |
| 1696 | |
| 1697 | // If we fall through to here we probably hit an assertion cast above |
| 1698 | // and assertions are not turned on. Anything we return is an error, so |
| 1699 | // BitCast is as good a choice as any. |
| 1700 | return BitCast; |
| 1701 | } |
| 1702 | |
| 1703 | //===----------------------------------------------------------------------===// |
| 1704 | // CastInst SubClass Constructors |
| 1705 | //===----------------------------------------------------------------------===// |
| 1706 | |
| 1707 | /// Check that the construction parameters for a CastInst are correct. This |
| 1708 | /// could be broken out into the separate constructors but it is useful to have |
| 1709 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 1710 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1711 | bool |
| 1712 | CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1713 | |
| 1714 | // Check for type sanity on the arguments |
| 1715 | const Type *SrcTy = S->getType(); |
| 1716 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType()) |
| 1717 | return false; |
| 1718 | |
| 1719 | // Get the size of the types in bits, we'll need this later |
| 1720 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1721 | unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); |
| 1722 | |
| 1723 | // Switch on the opcode provided |
| 1724 | switch (op) { |
| 1725 | default: return false; // This is an input error |
| 1726 | case Instruction::Trunc: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1727 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1728 | case Instruction::ZExt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1729 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1730 | case Instruction::SExt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1731 | return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1732 | case Instruction::FPTrunc: |
| 1733 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1734 | SrcBitSize > DstBitSize; |
| 1735 | case Instruction::FPExt: |
| 1736 | return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && |
| 1737 | SrcBitSize < DstBitSize; |
| 1738 | case Instruction::UIToFP: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1739 | return SrcTy->isInteger() && DstTy->isFloatingPoint(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1740 | case Instruction::SIToFP: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1741 | return SrcTy->isInteger() && DstTy->isFloatingPoint(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1742 | case Instruction::FPToUI: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1743 | return SrcTy->isFloatingPoint() && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1744 | case Instruction::FPToSI: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1745 | return SrcTy->isFloatingPoint() && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1746 | case Instruction::PtrToInt: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1747 | return isa<PointerType>(SrcTy) && DstTy->isInteger(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1748 | case Instruction::IntToPtr: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1749 | return SrcTy->isInteger() && isa<PointerType>(DstTy); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1750 | case Instruction::BitCast: |
| 1751 | // BitCast implies a no-op cast of type only. No bits change. |
| 1752 | // However, you can't cast pointers to anything but pointers. |
| 1753 | if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy)) |
| 1754 | return false; |
| 1755 | |
| 1756 | // Now we know we're not dealing with a pointer/non-poiner mismatch. In all |
| 1757 | // these cases, the cast is okay if the source and destination bit widths |
| 1758 | // are identical. |
| 1759 | return SrcBitSize == DstBitSize; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | TruncInst::TruncInst( |
| 1764 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1765 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1766 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | TruncInst::TruncInst( |
| 1770 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1771 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1772 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | ZExtInst::ZExtInst( |
| 1776 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1777 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1778 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | ZExtInst::ZExtInst( |
| 1782 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1783 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1784 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1785 | } |
| 1786 | SExtInst::SExtInst( |
| 1787 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1788 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1789 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 1792 | SExtInst::SExtInst( |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1793 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1794 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1795 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | FPTruncInst::FPTruncInst( |
| 1799 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1800 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1801 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | FPTruncInst::FPTruncInst( |
| 1805 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1806 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1807 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | FPExtInst::FPExtInst( |
| 1811 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1812 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1813 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | FPExtInst::FPExtInst( |
| 1817 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1818 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1819 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | UIToFPInst::UIToFPInst( |
| 1823 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1824 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1825 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | UIToFPInst::UIToFPInst( |
| 1829 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1830 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1831 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | SIToFPInst::SIToFPInst( |
| 1835 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1836 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1837 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | SIToFPInst::SIToFPInst( |
| 1841 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1842 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1843 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | FPToUIInst::FPToUIInst( |
| 1847 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1848 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1849 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | FPToUIInst::FPToUIInst( |
| 1853 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1854 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1855 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | FPToSIInst::FPToSIInst( |
| 1859 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1860 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1861 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | FPToSIInst::FPToSIInst( |
| 1865 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1866 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1867 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | PtrToIntInst::PtrToIntInst( |
| 1871 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1872 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1873 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | PtrToIntInst::PtrToIntInst( |
| 1877 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1878 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1879 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | IntToPtrInst::IntToPtrInst( |
| 1883 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1884 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1885 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | IntToPtrInst::IntToPtrInst( |
| 1889 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1890 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1891 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | BitCastInst::BitCastInst( |
| 1895 | Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore |
| 1896 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1897 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | BitCastInst::BitCastInst( |
| 1901 | Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd |
| 1902 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 1903 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1904 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 1905 | |
| 1906 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1907 | // CmpInst Classes |
| 1908 | //===----------------------------------------------------------------------===// |
| 1909 | |
| 1910 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 1911 | const std::string &Name, Instruction *InsertBefore) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1912 | : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1913 | Ops[0].init(LHS, this); |
| 1914 | Ops[1].init(RHS, this); |
| 1915 | SubclassData = predicate; |
| 1916 | if (op == Instruction::ICmp) { |
| 1917 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1918 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 1919 | "Invalid ICmp predicate value"); |
| 1920 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1921 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1922 | assert(Op0Ty == Op1Ty && |
| 1923 | "Both operands to ICmp instruction are not of the same type!"); |
| 1924 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1925 | assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1926 | "Invalid operand types for ICmp instruction"); |
| 1927 | return; |
| 1928 | } |
| 1929 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 1930 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 1931 | "Invalid FCmp predicate value"); |
| 1932 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1933 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1934 | assert(Op0Ty == Op1Ty && |
| 1935 | "Both operands to FCmp instruction are not of the same type!"); |
| 1936 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1937 | assert(Op0Ty->isFloatingPoint() && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1938 | "Invalid operand types for FCmp instruction"); |
| 1939 | } |
| 1940 | |
| 1941 | CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, |
| 1942 | const std::string &Name, BasicBlock *InsertAtEnd) |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1943 | : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1944 | Ops[0].init(LHS, this); |
| 1945 | Ops[1].init(RHS, this); |
| 1946 | SubclassData = predicate; |
| 1947 | if (op == Instruction::ICmp) { |
| 1948 | assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && |
| 1949 | predicate <= ICmpInst::LAST_ICMP_PREDICATE && |
| 1950 | "Invalid ICmp predicate value"); |
| 1951 | |
| 1952 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1953 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1954 | assert(Op0Ty == Op1Ty && |
| 1955 | "Both operands to ICmp instruction are not of the same type!"); |
| 1956 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1957 | assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1958 | "Invalid operand types for ICmp instruction"); |
| 1959 | return; |
| 1960 | } |
| 1961 | assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); |
| 1962 | assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && |
| 1963 | "Invalid FCmp predicate value"); |
| 1964 | const Type* Op0Ty = getOperand(0)->getType(); |
| 1965 | const Type* Op1Ty = getOperand(1)->getType(); |
| 1966 | assert(Op0Ty == Op1Ty && |
| 1967 | "Both operands to FCmp instruction are not of the same type!"); |
| 1968 | // Check that the operands are the right type |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1969 | assert(Op0Ty->isFloatingPoint() && |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1970 | "Invalid operand types for FCmp instruction"); |
| 1971 | } |
| 1972 | |
| 1973 | CmpInst * |
| 1974 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 1975 | const std::string &Name, Instruction *InsertBefore) { |
| 1976 | if (Op == Instruction::ICmp) { |
| 1977 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 1978 | InsertBefore); |
| 1979 | } |
| 1980 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 1981 | InsertBefore); |
| 1982 | } |
| 1983 | |
| 1984 | CmpInst * |
| 1985 | CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
| 1986 | const std::string &Name, BasicBlock *InsertAtEnd) { |
| 1987 | if (Op == Instruction::ICmp) { |
| 1988 | return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name, |
| 1989 | InsertAtEnd); |
| 1990 | } |
| 1991 | return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name, |
| 1992 | InsertAtEnd); |
| 1993 | } |
| 1994 | |
| 1995 | void CmpInst::swapOperands() { |
| 1996 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 1997 | IC->swapOperands(); |
| 1998 | else |
| 1999 | cast<FCmpInst>(this)->swapOperands(); |
| 2000 | } |
| 2001 | |
| 2002 | bool CmpInst::isCommutative() { |
| 2003 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2004 | return IC->isCommutative(); |
| 2005 | return cast<FCmpInst>(this)->isCommutative(); |
| 2006 | } |
| 2007 | |
| 2008 | bool CmpInst::isEquality() { |
| 2009 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2010 | return IC->isEquality(); |
| 2011 | return cast<FCmpInst>(this)->isEquality(); |
| 2012 | } |
| 2013 | |
| 2014 | |
| 2015 | ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) { |
| 2016 | switch (pred) { |
| 2017 | default: |
| 2018 | assert(!"Unknown icmp predicate!"); |
| 2019 | case ICMP_EQ: return ICMP_NE; |
| 2020 | case ICMP_NE: return ICMP_EQ; |
| 2021 | case ICMP_UGT: return ICMP_ULE; |
| 2022 | case ICMP_ULT: return ICMP_UGE; |
| 2023 | case ICMP_UGE: return ICMP_ULT; |
| 2024 | case ICMP_ULE: return ICMP_UGT; |
| 2025 | case ICMP_SGT: return ICMP_SLE; |
| 2026 | case ICMP_SLT: return ICMP_SGE; |
| 2027 | case ICMP_SGE: return ICMP_SLT; |
| 2028 | case ICMP_SLE: return ICMP_SGT; |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) { |
| 2033 | switch (pred) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2034 | default: assert(! "Unknown icmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2035 | case ICMP_EQ: case ICMP_NE: |
| 2036 | return pred; |
| 2037 | case ICMP_SGT: return ICMP_SLT; |
| 2038 | case ICMP_SLT: return ICMP_SGT; |
| 2039 | case ICMP_SGE: return ICMP_SLE; |
| 2040 | case ICMP_SLE: return ICMP_SGE; |
| 2041 | case ICMP_UGT: return ICMP_ULT; |
| 2042 | case ICMP_ULT: return ICMP_UGT; |
| 2043 | case ICMP_UGE: return ICMP_ULE; |
| 2044 | case ICMP_ULE: return ICMP_UGE; |
| 2045 | } |
| 2046 | } |
| 2047 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2048 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 2049 | switch (pred) { |
| 2050 | default: assert(! "Unknown icmp predicate!"); |
| 2051 | case ICMP_EQ: case ICMP_NE: |
| 2052 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2053 | return pred; |
| 2054 | case ICMP_UGT: return ICMP_SGT; |
| 2055 | case ICMP_ULT: return ICMP_SLT; |
| 2056 | case ICMP_UGE: return ICMP_SGE; |
| 2057 | case ICMP_ULE: return ICMP_SLE; |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | bool ICmpInst::isSignedPredicate(Predicate pred) { |
| 2062 | switch (pred) { |
| 2063 | default: assert(! "Unknown icmp predicate!"); |
| 2064 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2065 | return true; |
| 2066 | case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT: |
| 2067 | case ICMP_UGE: case ICMP_ULE: |
| 2068 | return false; |
| 2069 | } |
| 2070 | } |
| 2071 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2072 | FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) { |
| 2073 | switch (pred) { |
| 2074 | default: |
| 2075 | assert(!"Unknown icmp predicate!"); |
| 2076 | case FCMP_OEQ: return FCMP_UNE; |
| 2077 | case FCMP_ONE: return FCMP_UEQ; |
| 2078 | case FCMP_OGT: return FCMP_ULE; |
| 2079 | case FCMP_OLT: return FCMP_UGE; |
| 2080 | case FCMP_OGE: return FCMP_ULT; |
| 2081 | case FCMP_OLE: return FCMP_UGT; |
| 2082 | case FCMP_UEQ: return FCMP_ONE; |
| 2083 | case FCMP_UNE: return FCMP_OEQ; |
| 2084 | case FCMP_UGT: return FCMP_OLE; |
| 2085 | case FCMP_ULT: return FCMP_OGE; |
| 2086 | case FCMP_UGE: return FCMP_OLT; |
| 2087 | case FCMP_ULE: return FCMP_OGT; |
| 2088 | case FCMP_ORD: return FCMP_UNO; |
| 2089 | case FCMP_UNO: return FCMP_ORD; |
| 2090 | case FCMP_TRUE: return FCMP_FALSE; |
| 2091 | case FCMP_FALSE: return FCMP_TRUE; |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) { |
| 2096 | switch (pred) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2097 | default: assert(!"Unknown fcmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2098 | case FCMP_FALSE: case FCMP_TRUE: |
| 2099 | case FCMP_OEQ: case FCMP_ONE: |
| 2100 | case FCMP_UEQ: case FCMP_UNE: |
| 2101 | case FCMP_ORD: case FCMP_UNO: |
| 2102 | return pred; |
| 2103 | case FCMP_OGT: return FCMP_OLT; |
| 2104 | case FCMP_OLT: return FCMP_OGT; |
| 2105 | case FCMP_OGE: return FCMP_OLE; |
| 2106 | case FCMP_OLE: return FCMP_OGE; |
| 2107 | case FCMP_UGT: return FCMP_ULT; |
| 2108 | case FCMP_ULT: return FCMP_UGT; |
| 2109 | case FCMP_UGE: return FCMP_ULE; |
| 2110 | case FCMP_ULE: return FCMP_UGE; |
| 2111 | } |
| 2112 | } |
| 2113 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2114 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 2115 | switch (predicate) { |
| 2116 | default: return false; |
| 2117 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 2118 | case ICmpInst::ICMP_UGE: return true; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | bool CmpInst::isSigned(unsigned short predicate){ |
| 2123 | switch (predicate) { |
| 2124 | default: return false; |
| 2125 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 2126 | case ICmpInst::ICMP_SGE: return true; |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 2131 | switch (predicate) { |
| 2132 | default: return false; |
| 2133 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 2134 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 2135 | case FCmpInst::FCMP_ORD: return true; |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 2140 | switch (predicate) { |
| 2141 | default: return false; |
| 2142 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 2143 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 2144 | case FCmpInst::FCMP_UNO: return true; |
| 2145 | } |
| 2146 | } |
| 2147 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2148 | //===----------------------------------------------------------------------===// |
| 2149 | // SwitchInst Implementation |
| 2150 | //===----------------------------------------------------------------------===// |
| 2151 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2152 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2153 | assert(Value && Default); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2154 | ReservedSpace = 2+NumCases*2; |
| 2155 | NumOperands = 2; |
| 2156 | OperandList = new Use[ReservedSpace]; |
| 2157 | |
| 2158 | OperandList[0].init(Value, this); |
| 2159 | OperandList[1].init(Default, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2162 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2163 | : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()], |
| 2164 | SI.getNumOperands()) { |
| 2165 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 2166 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
| 2167 | OL[i].init(InOL[i], this); |
| 2168 | OL[i+1].init(InOL[i+1], this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2172 | SwitchInst::~SwitchInst() { |
| 2173 | delete [] OperandList; |
| 2174 | } |
| 2175 | |
| 2176 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2177 | /// addCase - Add an entry to the switch instruction... |
| 2178 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 2179 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2180 | unsigned OpNo = NumOperands; |
| 2181 | if (OpNo+2 > ReservedSpace) |
| 2182 | resizeOperands(0); // Get more space! |
| 2183 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2184 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2185 | NumOperands = OpNo+2; |
| 2186 | OperandList[OpNo].init(OnVal, this); |
| 2187 | OperandList[OpNo+1].init(Dest, this); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | /// removeCase - This method removes the specified successor from the switch |
| 2191 | /// instruction. Note that this cannot be used to remove the default |
| 2192 | /// destination (successor #0). |
| 2193 | /// |
| 2194 | void SwitchInst::removeCase(unsigned idx) { |
| 2195 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2196 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 2197 | |
| 2198 | unsigned NumOps = getNumOperands(); |
| 2199 | Use *OL = OperandList; |
| 2200 | |
| 2201 | // Move everything after this operand down. |
| 2202 | // |
| 2203 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 2204 | // client might not expect this to happen. The code as it is thrashes the |
| 2205 | // use/def lists, which is kinda lame. |
| 2206 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 2207 | OL[i-2] = OL[i]; |
| 2208 | OL[i-2+1] = OL[i+1]; |
| 2209 | } |
| 2210 | |
| 2211 | // Nuke the last value. |
| 2212 | OL[NumOps-2].set(0); |
| 2213 | OL[NumOps-2+1].set(0); |
| 2214 | NumOperands = NumOps-2; |
| 2215 | } |
| 2216 | |
| 2217 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 2218 | /// list according to the following behavior: |
| 2219 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 2220 | /// of operation. This grows the number of ops by 1.5 times. |
| 2221 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 2222 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 2223 | /// |
| 2224 | void SwitchInst::resizeOperands(unsigned NumOps) { |
| 2225 | if (NumOps == 0) { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2226 | NumOps = getNumOperands()/2*6; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2227 | } else if (NumOps*2 > NumOperands) { |
| 2228 | // No resize needed. |
| 2229 | if (ReservedSpace >= NumOps) return; |
| 2230 | } else if (NumOps == NumOperands) { |
| 2231 | if (ReservedSpace == NumOps) return; |
| 2232 | } else { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 2233 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | ReservedSpace = NumOps; |
| 2237 | Use *NewOps = new Use[NumOps]; |
| 2238 | Use *OldOps = OperandList; |
| 2239 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2240 | NewOps[i].init(OldOps[i], this); |
| 2241 | OldOps[i].set(0); |
| 2242 | } |
| 2243 | delete [] OldOps; |
| 2244 | OperandList = NewOps; |
| 2245 | } |
| 2246 | |
| 2247 | |
| 2248 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 2249 | return getSuccessor(idx); |
| 2250 | } |
| 2251 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 2252 | return getNumSuccessors(); |
| 2253 | } |
| 2254 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 2255 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2256 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2257 | |
| 2258 | |
| 2259 | // Define these methods here so vtables don't get emitted into every translation |
| 2260 | // unit that uses these classes. |
| 2261 | |
| 2262 | GetElementPtrInst *GetElementPtrInst::clone() const { |
| 2263 | return new GetElementPtrInst(*this); |
| 2264 | } |
| 2265 | |
| 2266 | BinaryOperator *BinaryOperator::clone() const { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2267 | return create(getOpcode(), Ops[0], Ops[1]); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2270 | CmpInst* CmpInst::clone() const { |
Reid Spencer | fcb0dd3 | 2006-12-07 04:18:31 +0000 | [diff] [blame] | 2271 | return create(getOpcode(), getPredicate(), Ops[0], Ops[1]); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2274 | MallocInst *MallocInst::clone() const { return new MallocInst(*this); } |
| 2275 | AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } |
| 2276 | FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); } |
| 2277 | LoadInst *LoadInst::clone() const { return new LoadInst(*this); } |
| 2278 | StoreInst *StoreInst::clone() const { return new StoreInst(*this); } |
| 2279 | CastInst *TruncInst::clone() const { return new TruncInst(*this); } |
| 2280 | CastInst *ZExtInst::clone() const { return new ZExtInst(*this); } |
| 2281 | CastInst *SExtInst::clone() const { return new SExtInst(*this); } |
| 2282 | CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); } |
| 2283 | CastInst *FPExtInst::clone() const { return new FPExtInst(*this); } |
| 2284 | CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); } |
| 2285 | CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); } |
| 2286 | CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); } |
| 2287 | CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); } |
| 2288 | CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); } |
| 2289 | CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); } |
| 2290 | CastInst *BitCastInst::clone() const { return new BitCastInst(*this); } |
| 2291 | CallInst *CallInst::clone() const { return new CallInst(*this); } |
| 2292 | ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); } |
| 2293 | SelectInst *SelectInst::clone() const { return new SelectInst(*this); } |
| 2294 | VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } |
| 2295 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 2296 | ExtractElementInst *ExtractElementInst::clone() const { |
| 2297 | return new ExtractElementInst(*this); |
| 2298 | } |
| 2299 | InsertElementInst *InsertElementInst::clone() const { |
| 2300 | return new InsertElementInst(*this); |
| 2301 | } |
| 2302 | ShuffleVectorInst *ShuffleVectorInst::clone() const { |
| 2303 | return new ShuffleVectorInst(*this); |
| 2304 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 2305 | PHINode *PHINode::clone() const { return new PHINode(*this); } |
| 2306 | ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); } |
| 2307 | BranchInst *BranchInst::clone() const { return new BranchInst(*this); } |
| 2308 | SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } |
| 2309 | InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } |
| 2310 | UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 2311 | UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} |