Longs are in one register on PowerPC 64; use appropriate instructions to operate...
[oota-llvm.git] / lib / Transforms / Utils / ValueMapper.cpp
1 //===- ValueMapper.cpp - Interface shared by lib/Transforms/Utils ---------===//
2 // 
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MapValue function, which is shared by various parts of
11 // the lib/Transforms/Utils library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ValueMapper.h"
16 #include "llvm/Constants.h"
17 #include "llvm/GlobalValue.h"
18 #include "llvm/Instruction.h"
19 #include <iostream>
20
21 using namespace llvm;
22
23 Value *llvm::MapValue(const Value *V, std::map<const Value*, Value*> &VM) {
24   Value *&VMSlot = VM[V];
25   if (VMSlot) return VMSlot;      // Does it exist in the map yet?
26   
27   // Global values do not need to be seeded into the ValueMap if they are using
28   // the identity mapping.
29   if (isa<GlobalValue>(V))
30     return VMSlot = const_cast<Value*>(V);
31
32   if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
33     if (isa<ConstantIntegral>(C) || isa<ConstantFP>(C) ||
34         isa<ConstantPointerNull>(C) || isa<ConstantAggregateZero>(C))
35       return VMSlot = C;           // Primitive constants map directly
36     else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
37       for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i) {
38         Value *MV = MapValue(CA->getOperand(i), VM);
39         if (MV != CA->getOperand(i)) {
40           // This array must contain a reference to a global, make a new array
41           // and return it.
42           //
43           std::vector<Constant*> Values;
44           Values.reserve(CA->getNumOperands());
45           for (unsigned j = 0; j != i; ++j)
46             Values.push_back(CA->getOperand(j));
47           Values.push_back(cast<Constant>(MV));
48           for (++i; i != e; ++i)
49             Values.push_back(cast<Constant>(MapValue(CA->getOperand(i), VM)));
50           return VMSlot = ConstantArray::get(CA->getType(), Values);
51         }
52       }
53       return VMSlot = C;
54
55     } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
56       for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
57         Value *MV = MapValue(CS->getOperand(i), VM);
58         if (MV != CS->getOperand(i)) {
59           // This struct must contain a reference to a global, make a new struct
60           // and return it.
61           //
62           std::vector<Constant*> Values;
63           Values.reserve(CS->getNumOperands());
64           for (unsigned j = 0; j != i; ++j)
65             Values.push_back(CS->getOperand(j));
66           Values.push_back(cast<Constant>(MV));
67           for (++i; i != e; ++i)
68             Values.push_back(cast<Constant>(MapValue(CS->getOperand(i), VM)));
69           return VMSlot = ConstantStruct::get(CS->getType(), Values);
70         }
71       }
72       return VMSlot = C;
73
74     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
75       if (CE->getOpcode() == Instruction::Cast) {
76         Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
77         return VMSlot = ConstantExpr::getCast(MV, CE->getType());
78       } else if (CE->getOpcode() == Instruction::GetElementPtr) {
79         std::vector<Constant*> Idx;
80         Constant *MV = cast<Constant>(MapValue(CE->getOperand(0), VM));
81         for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
82           Idx.push_back(cast<Constant>(MapValue(CE->getOperand(i), VM)));
83         return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx);
84       } else {
85         assert(CE->getNumOperands() == 2 && "Must be binary operator?");
86         Constant *MV1 = cast<Constant>(MapValue(CE->getOperand(0), VM));
87         Constant *MV2 = cast<Constant>(MapValue(CE->getOperand(1), VM));
88         return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2);
89       }
90
91     } else {
92       assert(0 && "Unknown type of constant!");
93     }
94   }
95
96   V->dump();
97   assert(0 && "Unknown value type: why didn't it get resolved?!");
98   return 0;
99 }
100
101 /// RemapInstruction - Convert the instruction operands from referencing the
102 /// current values into those specified by ValueMap.
103 ///
104 void llvm::RemapInstruction(Instruction *I,
105                             std::map<const Value *, Value*> &ValueMap) {
106   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
107     const Value *Op = I->getOperand(op);
108     Value *V = MapValue(Op, ValueMap);
109 #ifndef NDEBUG
110     if (!V) {
111       std::cerr << "Val = \n" << *Op << "Addr = " << (void*)Op;
112       std::cerr << "\nInst = " << *I;
113     }
114 #endif
115     assert(V && "Referenced value not in value map!");
116     I->setOperand(op, V);
117   }
118 }