My baby does the same thing, always has, she is almost 6 months old now. I do put her to sleep on her side swaddled and it seems to help a bit as she pushes her knees to her tummy. She usually tries to roll on her back when it happens too, I'm not sure if it's because of the pain or if she's trying to see me. I have no answers, sorry. I did start making her cereal/oatmeal with juice which has at least helped her with the constipation (she has always pooped only every few days but it got to be true constipation when she started oatmeal). She now squishy poos like a real baby!
Kate